我有这段代码来创建一个新的注册RegistrationController,和注册模型
const Registration = require('../models/Registration');
module.exports = {
async create(request, response) {
const { user_id } = request.headers;
const { eventId } = request.params;
const { date } = request.body;
const registration = await Registration.create({
user: user_id,
event: eventId,
date
});
await registration
.populate('event')
.populate('user', '-password')
.execPopulate(); //PROBLEM HERE
return response.json(registration);
},
const mongoose = require('mongoose');
const RegistrationSchema = new mongoose.Schema({
date: String,
approved: Boolean,
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
event: {
type: mongoose.Schema.Types.ObjectId,
ref: "Event"
}
});
module.exports = mongoose.model('Registration', RegistrationSchema);
当我发送一个post请求到insomnia时,显示这个错误:有人能帮帮忙吗
2条答案
按热度按时间col17t5w1#
您需要
.find
或.findOne
,它将从DB中检索并填充刚刚创建的文档:4jb9z9bj2#
我也有同样的问题