0

I'm creating a post request which inserts multiple values into my table, but I'm getting a 'column count doesn't match value count at row 1' error

I know this error is triggered when the number of values you provide in a query doesn't match the number of columns. However, I can't find the source of my issue. My query roughly looks like this:

db.connection('INSERT into Surveys (name, location, options) VALUES (?,?,?)', [req.name, req.location, req.options], .....

One of my SQL columns (named 'options') is a JSON column. My app's front end will provide the data like this: {choices: ["Option 1","Option 2","Option 3","Option 4"]}.

If this options value is null, the query works perfectly. However, if the options value is in the JSON format depicted above, the error is triggered. Note: I'm able to manually enter the JSON using mySQL Workbench with no issues.

Any theories on what could be going on?

3
  • 1
    stackoverflow.com/a/40931111/10079978 i think this answer will help you. Commented Oct 13, 2022 at 8:12
  • 1
    Firstly try to convert req.options to string explicitly. Commented Oct 13, 2022 at 8:24
  • Turning it to a string did the trick! Wish I thought of this sooner. Thank you! Commented Oct 13, 2022 at 21:04

0

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.