我正在创建一个简单的MERN堆栈项目-用户使用包含ObjectId用户引用的笔记模型做笔记,同时测试端点,它给了我一个没有响应的请求发送,我需要知道为什么。
//Note model
const mongoose = require('mongoose');
const AutoIncrement = require('mongoose-sequence')(mongoose);
//create employee schema
const noteSchema = new mongoose.Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User',
},
title: {
type: String,
required: [true, 'title note is required!'],
},
text: {
type: String,
required: [true, 'note description is required!'],
},
completed: {
type: Boolean,
default: false,
},
},
{
timestamps: true,
}
);
noteSchema.plugin(AutoIncrement, {
inc_field: 'ticket',
id: 'ticketNums',
start_seq: 500,
});
module.exports = mongoose.model('Note', noteSchema);
createNote控制器代码:
请注意,req.id是从jwt访问令牌获取的。
const createNote = asyncHandler(async (req, res) => {
const { title, text } = req.body;
if (!title || !text)
return res.status(400).json({
message: 'title and text are required for creating a new note.!',
});
const note = await Note.create({
user: req.id,
title,
text,
});
return res.status(201);
});
1条答案
按热度按时间lymnna711#
如果你仍然没有发现问题,只需在控制器的末尾添加你想要返回的数据。
写这个:
其他一切似乎都很好。