Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

A POST method API Express Node.JS not working, SyntaxError: Unexpected token ' in JSON at position 68 at JSON.parse (<anonymous>)

I’m trying to make this post API work, its an API to post some data to a table in my database, but every time I tried it on postman it gives me this kind of error:

SyntaxError: Unexpected token ' in JSON at position 68
    at JSON.parse (<anonymous>)
    at parse (C:\javascript-projects\living-main\API\node_modules\express\node_modules\body-parser\lib\types\json.js:89:19)
    at C:\javascript-projects\living-main\API\node_modules\express\node_modules\body-parser\lib\read.js:128:18
    at AsyncResource.runInAsyncScope (node:async_hooks:203:9)
    at invokeCallback (C:\javascript-projects\living-main\API\node_modules\express\node_modules\raw-body\index.js:231:16)
    at done (C:\javascript-projects\living-main\API\node_modules\express\node_modules\raw-body\index.js:220:7)
    at IncomingMessage.onEnd (C:\javascript-projects\living-main\API\node_modules\express\node_modules\raw-body\index.js:280:7)
    at IncomingMessage.emit (node:events:525:35)
    at endReadableNT (node:internal/streams/readable:1358:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

This is my function code for the insert query in the models folder:

const dbpool = require('../config/database')

const createNewRecruitment = () => {
  const SQLQuery = `INSERT INTO tb_recruitment_teams () 
                    VALUES (
                      '${body.identifier_recruitment_team}',
                      '${body.name_team}',
                      '${body.post_team}',
                      '${body.domicile_team}',
                      '${body.job_description}',
                      '${body.experience}',
                      '${body.certificate}'
                      )`;
  return dbpool.execute(SQLQuery);
}
module.exports = {
  createNewRecruitment,
}

My POST code in recruitment.controllers.js

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const ProjectModel = require('../models/tb_recruitment_team')

const createNewRecruitment = async (req, res) => {
    const {body} = req;
    try{
        await ProjectModel.createNewRecruitment(body);
        res.json({
            message: 'Create New Recruitment Success',
            data: body
        })
    }catch (error) {
        res.status(500).json({
            message: 'Server Error',
            ServerMessage: error,
        })
    }    
}

module.exports = {
   createNewRecruitment,
}

My routes in recruitment.js

const express = require('express');
const RecruitmentControllers = require('../controllers/recruitment.controllers');

const router = express.Router();

//CREATE - POST
router.post('/', RecruitmentControllers.createNewRecruitment);

module.exports = router;

and my app.js

const express = require('express');
const app = express();

const recruitmentRoute = require('./routes/recruitmen.js');

const middlewareLogRequest = require('./middleware/log.js')

app.use(middlewareLogRequest);

app.use(express.json({ strict: false }))

app.use('/recuitments', recruitmentRoute);

module.exports = app;

and this is the body JSON that I use for testing my POST in Postman:

{
            "identifier_recruitment_team": '34',
            "name_team": 'NTDD',
            "post_team": 'Balikpapan',
            "domicile_team": 'Job Description Contributor',
            "job_description": 'ERTBB',
            "experience": 'ERTBB',
            "certificate": 'ERTBB'
}

Did I do something wrong? I’ve been searching for it but I still didn’t get it.

The POST method to save data work and will give the right res.json.

>Solution :

I think your problem is that your JSON body is invalid, JSON doesn’t accept single quotes ' ', use instead double quotes " " for your values

Try this:

{
            "identifier_recruitment_team": "34",
            "name_team": "NTDD",
            "post_team": "Balikpapan",
            "domicile_team": "Job Description Contributor",
            "job_description": "ERTBB",
            "experience": "ERTBB",
            "certificate": "ERTBB"
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading