Reputation: 157
I am trying to INSERT data in a table of MYSql using NodeJs, Express framework throw Postman with the POST method. I get an error. I dont know if it's about the parse of the json. or i have to send it different.
Json sended from postman(raw, JSON):
{
"namel": "Teusaquillo",
"area_2m": 6000,
"parent": "chapinero"
}
When i send the request i get this error:
<pre>SyntaxError: Unexpected token in JSON at position 3<br> at JSON.parse (<anonymous>)<br> at parse (<mypaht>\nodeapi\node_modules\body-parser\lib\types\json.js:89:19)<br> at <mypaht>\node_modules\body-parser\lib\read.js:121:18<br> at invokeCallback (<mypaht>nodeapi\node_modules\raw-body\index.js:224:16)<br> at done (<mypaht>\node_modules\raw-body\index.js:213:7)<br> at IncomingMessage.onEnd (<mypaht>\node_modules\raw-body\index.js:273:7)<br> at IncomingMessage.emit (events.js:326:22)<br> at endReadableNT (_stream_readable.js:1252:12)<br>  `; at processTicksAndRejections (internal/process/task_queues.js:80:21)</pre>
Other view of the error:
SyntaxError: Unexpected token in JSON at position 3
at JSON.parse (<anonymous>)
at parse (<mypaht>\node_modules\body-parser\lib\types\json.js:89:19)
at <mypaht>\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (<mypaht>\node_modules\raw-body\index.js:224:16)
at done (<mypaht>\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (<mypaht>\node_modules\raw-body\index.js:273:7)
at IncomingMessage.emit (events.js:326:22)
at endReadableNT (_stream_readable.js:1252:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
This is the index.js file:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const mysql = require('mysql');
app.use(bodyParser.json());
//create database connection
const conn = mysql.createConnection({
...
});
//connect to database
conn.connect((err) =>{
...
});
app.post('/api/locations',(req, res) => {
let data = {namel: req.body.namel, area_2m: req.body.area_2m, parent: req.body.parent};
let sql = "INSERT INTO location SET ?";
let query = conn.query(sql, data,(err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
This is the table i am trying to fill:
CREATE TABLE `location` (
`namel` char(100) NOT NULL,
`area_2m` int(11) DEFAULT NULL,
`parent` char(100) DEFAULT NULL,
PRIMARY KEY (`namel`),
KEY `parent` (`parent`),
CONSTRAINT `location_ibfk_1` FOREIGN KEY (`parent`) REFERENCES `location` (`namel`)
)
I'll apreciate your help. Thanks!
Upvotes: 0
Views: 376
Reputation: 502
Data passed on query should an array?
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const mysql = require('mysql');
app.use(bodyParser.json());
//create database connection
const conn = mysql.createConnection({
...
});
//connect to database
conn.connect((err) =>{
...
});
app.post('/api/locations',(req, res) => {
// use destruction for easy reading
let { namel, area_2m, parent } = req.body;
// use mysql format to build query
let sql = "INSERT INTO location SET ?";
let value = [namel, area_2m, parent]
let queryString = mysl.format(sql, value)
let query = conn.query(queryString,(err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
Upvotes: 1
Reputation: 31
In my case this happened because of extra white space. Once I got rid of all extra white spaces between properties and between the opening parenthesis and the first key/property, it worked.
I am running NodeJS with express and I guess express.json
throws an error instead of trimming out extra whitespace even though extra whitespace in json is perfectly valid json.
Upvotes: 0