猫鼬(MERN)中的findOneAndUpdate

时间:2020-07-27 02:38:21

标签: mern

在编写简短教程时出现了一个小瓶颈: 我希望能够推这个模型:

    const studentFatherSchema = new Schema ({
        yam:{
            type: String,
            trim: true
        },
        emailmanagement: {
            type: String,
            trim: true
        },
        students: [{
            name: {
                type: String,
                trim: true
            }
        }]
    });

我首先必须使用findById (req.params.studentFatherId)找到父对象,然后进行更新,还是可以一次全部完成?我正在尝试一次完成所有操作,但是我不能,您有何建议? 注意:我可以毫无问题地更新名称和emailmanagement字段,但是使用推送进行更新不会将其添加到我中,我正在使用Postman和

对其进行测试

我有类似的东西,但是它不起作用!

    exports.updateAddChild = async (req, res, next) => {
        try {
            const student = req.body;
            const studentFather = await StudentFather.findOneAndUpdate (
                {_id: req.params.studentFatherId},
                {$ push: {students: student}},
            );
        } catch (error) {
            console.log (error);
            next ();
        }
    }

非常感谢您的帮助!

0 个答案:

没有答案