SyntaxError:JSON中位置102处出现意外令牌}

乔丹·罗布

我创建了一个连接到mongo atlas的简单节点/表达api,我尝试发送发布请求以使用vscode中的其余客户端扩展将新数据添加到数据库中,但继续遇到上述错误,不确定我的代码在哪里搞砸了

带有中间件的主服务器文件

const express = require("express");
const app = express();
const coolapi = require("./api/coolapi");
const dotenv = require("dotenv");
const mongoose = require("mongoose");

dotenv.config();

mongoose.connect(process.env.DB_ACCESS, { useUnifiedTopology: true }, () => {
  console.log("DB connected");
});

app.use(express.json());

app.use("/api/coolapi", coolapi);

app.listen(3000, () => {
  console.log("server is running");
});

快速路由器文件

const express = require("express");
const router = express.Router();
const coolioObj = require("../models/CoolModel");

router.post("/signup", (request, response) => {
  const coolPerson = new coolioObj({
    username: request.body.username,
    email: request.body.email,
    password: request.body.password,
  });
  coolPerson
    .save()
    .then((data) => {
      response.json(data);
    })
    .catch((error) => {
      response.json(error);
    });
});

module.exports = router;

猫鼬模式的文件

const mongoose = require("mongoose");

const coolioSchema = mongoose.Schema({
  username: {
    type: String,
    required: true,
  },

  email: {
    type: String,
    required: true,
  },

  password: {
    type: String,
    required: true,
  },

  date: {
    type: Date,
    default: Date.now,
  },
});

module.exports = mongoose.model("coolioDB", coolioSchema);

请求发送

POST http://localhost:3000/api/coolapi/signup http/1.1
Content-Type: application/json

{
    "username":"samthingsoweto",
    "email":"[email protected]",
    "password":"KabzaDaSmall",
}

亚杜尔

JSON数组在最后一项之后不支持逗号。这就是错误的原因。删除所有JSON数组中的最后一个逗号。

 date: {
    type: Date,
    default: Date.now,
  }, <---
{
    "username":"peoplefrom",
    "email":"[email protected]",
    "password":"peoplepassword123", <---
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

SyntaxError:JSON中位置0处出现意外令牌'

SyntaxError:JSON中位置0处出现意外令牌“

Google App脚本-SyntaxError:JSON中位置1处出现意外令牌-映射背景颜色数组时

SyntaxError:JSON位置0处出现意外的令牌C-Ionic 2 Http GET请求

Javascript JSON解析错误:JSON中位置1处出现意外令牌'

NodeJS SyntaxError:JSON中位置16处出现意外字符串

ionic 2中的错误“错误:JSON在位置1923处出现意外令牌'}'”是什么

SyntaxError:JSON中出现意外的令牌\

SyntaxError:Object.parse处出现意外令牌(本机)

SyntaxError:JSON中位置1处的意外令牌o

“ SyntaxError:JSON中位置0处的意外令牌<”

NodeJS SyntaxError:JSON中位置0处的意外令牌

SyntaxError:JSON中位置1 EXPRESS处的意外令牌o

Firebase FCM错误JSON_PARSING_ERROR:位置处出现意外的令牌END OF FILE

Java Eclipse - 尝试从 JSON 读取时“位置 0 处出现意外令牌 END OF FILE”

TypeScript错误TS5014:JSON在位置0处出现意外令牌u

查询API时,JSON在位置0处出现意外令牌O

WordPress:在使用 Duplicator 复制页面后,位置 0 处的 JSON 中出现意外令牌 <

html标记处出现意外令牌}

从JSON中的csv文件中提取值[SyntaxError:JSON中位置0处的意外令牌a]

Wordpress/woocommerce:当用户登录时重定向,导致位置 0 处的 JSON 中出现意外令牌 <

使用FS读取字符串会返回此错误:JSON在位置0处出现意外令牌r

JSON.parse()导致错误:`SyntaxError:JSON中位置0处的意外令牌`

SyntaxError:如果出现意外令牌

实体错误:位置JSON中出现意外的令牌h

React Js:未捕获(在promise中)SyntaxError:JSON中位置0处的意外令牌<

在create-react-app中配置生产环境(SyntaxError:JSON中位置0处的意外令牌<)

SyntaxError:Ajax请求中位置1处的JSON中的意外令牌o

SyntaxError:Object.parse(本机)AngularJS处出现意外令牌o