事件集合中已经有一些文档,但是当我尝试注销“ eventId”字段时,它显示“未定义”。 以下是代码:
var EventSchema = mongoose.Schema({
eventId: { type: Number, required: true,unique: true },
name: { type: String, required: true },
loc: { type: ObjectId },
quota: { type: Number }
});
var Event = mongoose.model('Event', EventSchema);
app.get('/test',function(req,res){
Event.find(function(err,result){
if(err)
console.log("Not found");
console.log("This is" + result.eventId);
res.send(result);
});
});
答案 0 :(得分:0)
尝试:
var EventSchema = mongoose.Schema({
eventId: { type: Number, required: true,unique: true },
name: { type: String, required: true },
loc: { type: ObjectId },
quota: { type: Number }
});
var Event = mongoose.model('Event', EventSchema);
app.get('/test', async function(req,res){
const result = await Event.find();
console.log("This is the result: " + result);
res.send(result);
});
我认为您的问题是回调位于.find()方法的过滤器对象位置。以下可能也可以:
var EventSchema = mongoose.Schema({
eventId: { type: Number, required: true,unique: true },
name: { type: String, required: true },
loc: { type: ObjectId },
quota: { type: Number }
});
var Event = mongoose.model('Event', EventSchema);
app.get('/test',function(req,res){
Event.find({}, function(err,result){
if(err) console.log("Not found");
console.log("This is" + result.eventId);
res.send(result);
});
});