猫鼬不推入数组

时间:2019-02-02 15:19:04

标签: javascript node.js mongodb express mongoose

我的代码是:

型号:

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(“推”)),它不把数据在那里?我想念什么?

1 个答案:

答案 0 :(得分:0)

正在使用错误的密钥,使用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
        })
    });