0

I want to make GET request that returns data from Postgres database without specifying in advanced the where clause.

router.get("/:object_query", async (req, res) => {
        var object_query = req.params.object_query;
        var note = await db.note.findAll({
            where: object_query
        });
        if (!note.length) {
            return res.status(404).send();
        }
        res.send({
            note
        });
    });

Call for the request would be something like this:

localhost/note/{objectID: "1234"}

or

localhost/note/{objectID: "1234", otherparam: "abcd"}

Idea is that I can from same request query table with different rows.

Is something like this possible without writing request for each different number of params I plan to use for where clause, as I had an idea? How should it be done, from my tries I got

(node:23832) UnhandledPromiseRejectionWarning: Error: Support for `{where: 'raw query'}` has been removed.

Thank you for help.

2 Answers 2

1

req.params.object_query is JSON string, you can use JSON.parse function

var object_query = JSON.parse(req.params.object_query);

and url must be like localhost/note/{"objectID": 1234, "otherparam": "abcd"}

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for help, it worked, i was missing parsing the object
0

This should work -

  router.get("/:object_query", async (req, res) => {
       var object_query = req.params.object_query;
       db.note.findAll({
            where: JSON.parse(object_query)
       }).then(notes => {
          if (!notes.length) {
              return res.status(404).send();
          }
          res.send({
              notes
          });
       });
   });

Make sure that param is a valid JSON.

1 Comment

Thank you for help, it worked, i was missing parsing the object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.