0

I am trying to add a 'post question' function that makes a post request to my Postgres API/database.

This is my function:

 const postQuestion = (req, res) => {

        let postVals = [req.params.id, Object.values(req.body)].flat();
        let p_id = Number(req.params.id);
        let q_body = postVals[1]
        let q_date = postVals[2];
        let name = postVals[3];
        let help = postVals[4];
        let reported = postVals[5];
        
        pool.query(`SET search_path TO qa, public; \
        INSERT INTO questions(product_id, question_body, question_date, asker_name, helpfulness, reported) \
        VALUES ($1, $2, $3, $4, $5, $6)`, [p_id, q_body, q_date, name, help, reported], (err) => {
          if (err) {
            console.log(err)
          }
          res.send('success')
        })    
      }

And I'm testing it with Postman which is where the req.body comes in from.

This is my schema....

CREATE TABLE questions (
   question_id SERIAL PRIMARY KEY,
   product_id INT,
   question_body VARCHAR(255),
   question_date VARCHAR(255),
   asker_name VARCHAR(255),
   helpfulness INT,
   reported BOOLEAN
);

and I'm getting this error:

error: cannot insert multiple commands into a prepared statement at Parser.parseErrorMessage (/Users/megan/Boulder/SDC/ClarkFECSource/node_modules/pg-protocol/dist/parser.js:278:15)

1
  • 1
    Try separating the SET and INSERT queries. (note that I found this by googling the error message) Commented Dec 8, 2020 at 0:14

1 Answer 1

0

The error is caused by the semicolon, or rather, by the fact that your query string contains two statements.

Simply omit the SET search_path and modify the INSERT to INSERT INTO qa.questions.

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

Comments

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.