gpt4 book ai didi

javascript - Mongoose 没有插入数组

转载 作者:可可西里 更新时间:2023-11-01 10:45:15 26 4
gpt4 key购买 nike

我的代码是:

型号:

var mongoose = require('mongoose');

var eventSchema = new mongoose.Schema({
'eventTitle': String,
'location': String,
'startDate': String,
'endDate': String,
'startTime': String,
'endTime': String,
'createdBy': mongoose.Schema.Types.ObjectId, // Here we will store the _ID from the user EOSP.
'attendants': {
'seekers': [mongoose.Schema.Types.ObjectId],
'employers': [],
},
'isFinished': {'type': Boolean, 'default': false},
'uploadedResumes': Number,
'downloadedResumes': Number,
'survey': {
'seekers': [],
'employers': [],
'host': [],
}
});

module.exports = mongoose.model('event', eventSchema);

路由器:

.post(async (req, res) => {
let {user, params} = req;
let {eid, uid} = params;
eid = mongoose.Types.ObjectId(eid);
uid = mongoose.Types.ObjectId(uid);

user = user ? user : await Account.findById(uid).catch(e => console.log(e));
if (user.accType.toLowerCase() === 'seeker') {
const {
rating,
recommendation,
websiteEaseOfUse,
chatHelpfulness,
skillsSuitability,
tips,
} = req.body;
const data = {
userId: user._id,
rating,
recommendation,
websiteEaseOfUse,
chatHelpfulness,
skillsSuitability,
tips,
};
console.log(data);
Event.findOneAndUpdate({'_id': eid}, {
$push: {
'survey.seeker': {
userId: user._id,
rating,
recommendation,
websiteEaseOfUse,
chatHelpfulness,
skillsSuitability,
tips,
},
},
}, {$upsert: true,}).then(r => console.log('pushed', r.survey.seekers)).catch(e => console.log(e));
}
const title = await Event.findById(eid).then(r => r.eventTitle).catch(e => console.log(e));
const event = await functions.getCurrentEvent();
res.render('survey/thanks', {
title: title,
user: user,
event: event
})
});

所以这段代码应该做的是通过 post 将数据传递给路由器(这实际上有效),并将其推送到一个数组中(这不起作用)。

这是日志:

pushed []

所以我的问题是为什么即使控制台说它通过了 (console.log('push')),它也没有将数据放在那里?我错过了什么?

最佳答案

您使用了错误的 key ,请使用 survey.seekers 而不是 survey.seeker

  .post(async (req, res) => {
let {user, params} = req;
let {eid, uid} = params;
eid = mongoose.Types.ObjectId(eid);
uid = mongoose.Types.ObjectId(uid);

user = user ? user : await Account.findById(uid).catch(e => console.log(e));
if (user.accType.toLowerCase() === 'seeker') {
const {
rating,
recommendation,
websiteEaseOfUse,
chatHelpfulness,
skillsSuitability,
tips,
} = req.body;
const data = {
userId: user._id,
rating,
recommendation,
websiteEaseOfUse,
chatHelpfulness,
skillsSuitability,
tips,
};
console.log(data);
Event.findOneAndUpdate({'_id': eid}, {
$push: {
'survey.seekers': {
userId: user._id,
rating,
recommendation,
websiteEaseOfUse,
chatHelpfulness,
skillsSuitability,
tips,
},
},
}, {$upsert: true,}).then(r => console.log('pushed', r.survey.seekers)).catch(e => console.log(e));
}
const title = await Event.findById(eid).then(r => r.eventTitle).catch(e => console.log(e));
const event = await functions.getCurrentEvent();
res.render('survey/thanks', {
title: title,
user: user,
event: event
})
});

关于javascript - Mongoose 没有插入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54494423/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com