简体   繁体   中英

Node.JS SyntaxError: Unexpected token i in JSON at position 20

Something weird happened with my code || environment. The following code was working fine yesterday.

app.get('/', async (req, res)=>{
    const catalog = {
        kitchen: await knex('kitchen').distinct('item_group'),
        bedroom: await knex('bedroom').distinct('item_group'),
        living_room: await knex('living_room').distinct('item_group'),
        kidsroom: await knex('kidsroom').distinct('item_group'),
        bathroom: await knex('bathroom').distinct('item_group'),
        accessories: await knex('accessories').distinct('item_group')
    }
    res.json(catalog)
})

The next day, GET request replies with:

SyntaxError: Unexpected token i in JSON at position 20
    at JSON.parse (<anonymous>)
    at parse (C:\Users\User\Desktop\Apps\SmartWebsite\node_modules\body-parser\lib\types\json.js:89:19)
    at C:\Users\User\Desktop\Apps\SmartWebsite\node_modules\body-parser\lib\read.js:121:18
    at invokeCallback (C:\Users\User\Desktop\Apps\SmartWebsite\node_modules\raw-body\index.js:224:16)
    at done (C:\Users\User\Desktop\Apps\SmartWebsite\node_modules\raw-body\index.js:213:7)
    at IncomingMessage.onEnd (C:\Users\User\Desktop\Apps\SmartWebsite\node_modules\raw-body\index.js:273:7)
    at IncomingMessage.emit (events.js:412:35)
    at endReadableNT (internal/streams/readable.js:1317:12)
    at processTicksAndRejections (internal/process/task_queues.js:82:21)

I have the following frameworks/dependencies installed:

const express = require('express')
const mysql = require('mysql2/promise')
const app = express()
const port = 3000

app.use(express.json())

The problem is happening with all projects in my computer which were working fine so far.

I tried:

  • restarting computer;)
  • uninstalling and reinstalling frameworks and dependencies
  • JSON.parse(object)
  • JSON.stringify(object)

Thank you in advance for your help!

First of all

const catalog = {
        kitchen: await knex('kitchen').distinct('item_group'),
        accessories: await knex('accessories').distinct('item_group')
        ...
    }

instead, assign each of the query to a new variable, then check if they are all working by simply console logging.

The problem seemed to be with Postman's cache memory. After sending a GET request from the browser everything was back in order. I closes old windows in Postman and sent new requests from new windows. It is working fine again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM