简体   繁体   English

JSON 输入意外结束:SyntaxError

[英]Unexpected end of JSON input : SyntaxError

I keep getting Unexpected end of JSON input error when I test and it keeps crashing我在测试时不断收到 Unexpected end of JSON input 错误,并且一直崩溃

import express from 'express';
import mongoose from 'mongoose';
import Messages from './dbMessages.js'

const app = express ()
const port = process.env.PORT || 9000;

app.use(express.json());

const connection_url = '...'

mongoose.connect(connection_url, {
    useNewUrlParser: true,
    useUnifiedTopology: true
})

app.get('/', (req, res)=> res.status(200).send('hello world'))

app.get('/messages/sync', (req, res) => {
    Messages.find((err, data) => {
        if(err) {
            res.status(500).send(err)
        } else {
            res.status(200).send(data)
        }
    })
})

app.post('/messages/new', (req, res) => {
    const dbMessage = req.body

    Messages.create(dbMessage, (err, data) => {
        if (err) {
            res.status(500).send(err)
        } else {
            res.status(201).send(data)
        }
    })
})

app.listen(port, ()=>console.log(`Listening on localhost:${port}`))

with all these I don't know why I keep getting an error each time I test the get method using postman,有了所有这些,我不知道为什么每次使用邮递员测试 get 方法时都会出现错误,

The error is likely coming from the client side.该错误可能来自客户端。 Do check the format of your data from the client side.请从客户端检查数据的格式。 For example, if you are using postman to test, make sure you do not add a comma at the end of your json data.例如,如果您使用 postman 进行测试,请确保不要在 json 数据的末尾添加逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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