我正在尝试创建 Note.js(模型)的实例“note”
通过 postman raw json 发送一个 post 请求
{
"user": "645xxxxxxxxxxxxxxxxxxx88d",
"title": "title",
"text": "description"
}
卡在了线上:
const note = await Note.create({ user })
在笔记控制器中
此行之后有一个 if(note) 返回响应,但它似乎没有到达那里。
我认为代码很好,因为我一直在学习教程。 我在 server.js 文件中确实有
app.use(express.json());
。
console.log(Note.create(noteObject))
输出 Promise { <pending> }
我尝试了不同的语法,但都导致了错误,一个突出的错误是 BSONError。
更新:
很明显,注释掉这个 Node.js 文件中的注释代码可以让它正常运行。但我不应该这样做。出了什么问题,如何解决?
const mongoose = require('mongoose')
const AutoIncrement = require('mongoose-sequence')(mongoose)
const noteSchema = new mongoose.Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
title: {
type: String,
required: true
},
text: {
type: String,
required: true
},
completed: {
type: Boolean,
default: false
}
},
{
timestamps: true
}
)
//noteSchema.plugin(AutoIncrement, {
// inc_field: 'ticket',
// id: 'ticketNums',
// start_seq: 500
//})
module.exports = mongoose.model('Note', noteSchema)