猫鼬:我的findByIdAndUpdate有什么问题?

时间:2019-10-15 17:19:20

标签: javascript node.js mongodb express mongoose

我的想法很简单,它是从req.body获取更新值,但确实可以正常工作,mongodb中的数据永不更改。

已经尝试过{$ set:电子邮件,密码}

    const id = { _id: req.params.id };
    const { email, password } = req.body;

    let user = await User.findByIdAndUpdate(id, { email, password });

    if (!user) {
        return res.json({ message: "error"});
    }

    return res.json({
        user: user,
        updated: true
    })

2 个答案:

答案 0 :(得分:3)

当对象只需要一个字符串/ ObjectId时,便将对象传递到id字段

const { email, password } = req.body;

let user = await User.findByIdAndUpdate(req.params.id, { email, password });

if (!user) {
    return res.json({ message: "error"});
}

return res.json({
    user: user,
    updated: true
})

答案 1 :(得分:0)

表达式:

    let user = await User.findByIdAndUpdate(id._id, { email, password });

创建一个名为id的对象,其中_id字段设置为参数中的实际id值。 因此,最终您要做的是将整个对象而不是id传递给findByIdAndUpdate的第一个参数,该参数仅接受id作为第一个参数。 如果仍要使用该对象,只需在findByIdAndUpdate方法中将id替换为id._id。然后您将其写为:

Mydataframe__df.to_csv(string_io, sep=',', quoting=csv.QUOTE_ALL, header=True, index=False , encoding='utf-8')
df_writer = Mydata_Output.get_writer('/MYFILE_TEST.csv')
df_string = string_io.getvalue()

# save the string as bytes to with the writer
df_writer.write(df_string.encode('utf-8'))

# close the writer connection
df_writer.close()