Why do I receive typeError when trying to parse query from routing to Json for search?

Viewed 28

I try to implement a search function using deno oak as a webapp and mongodb as a database. I got the routing and correct and can put out the code on the console or to the web using the following class:

export class Helper {
    public parseSearchKeys(searchKeys: any) {
        console.log(`Receiving the following search Query: ${JSON.stringify(searchKeys)}`)
        return searchKeys;
    }
};

and this function directly implemented on the router:

app.get("/queryparamsdemo", function (req, res) {
    const helperService = new Helper()
    const searchQuerymongo = Helper.parseSearchKeys({ ID: req.query.id, length: req.query.length })
    console.log(`i parsed the following searchQuery: ${searchQuerymongo}`)
});

however when i try to extend to the search on the database using the following function:

const searchItem = async ({
  params,
  response,
}: {
  params: any;
  response: any;
}) => {

  const helper = new Helper();
  const searchQuery = helper.parseSearchKeys({ id: params.query.id, length: params.query.length });
  const searched = await collection.find({searchQuery}, { noCursorTimeout: false }).toArray();
};

i get this error:

[uncaught application error]: TypeError - Cannot read properties of undefined (reading 'title')

request: {
  url: "http://localhost:4000/api/trails/searchKeys?title=%22erste%20Trail%22&length=32",
  method: "GET",
  hasBody: false
}

The problem lies in the passing of the parameters in the function search item. Static search is possible and yields correct results.

Sorry for the long explanation, i was trying to be very precise.

What am I doing wrong?

0 Answers
Related