Ruwan Bandara
Ruwan Bandara

Reputation: 65

how to insert query and save json data into sql server using nodejs

I am trying to insert query into SQL Server using window authentication with nodejs. I have done with get request of select query. but now am trying the post request with insert query. But I can't pass my req.body.address into the following query. the address data have json value.

here my code enter image description here

here my request data,

Request data

the sql table row, Sql tabel address row

That is the error, enter image description here

Upvotes: 0

Views: 5017

Answers (2)

Nits
Nits

Reputation: 550

You need to save as string in user_address field.

eg: If you want to save as address like this:

user_address: `${req.body.address.street},${req.body.address.district},${req.body.address.city}`

Or

user_address: JSON.stringify(req.body.address) 

whenever you want to show the address you need to do JSON.parse(user_address).

In nutshell, value must be a single value.

Upvotes: 1

Rashomon
Rashomon

Reputation: 6762

Your mysql library is probably, by default, applying a standard string conversion to req.body.address. When you do this to a javascript object you obtain [object Object]:

req.body.address.toString() // "[object Object]"

Objects needs to be converted to string using JSON.stringify():

"user_address": JSON.stringify(req.body.address)

Upvotes: 1

Related Questions