gpt4 book ai didi

node.js - 使用 mongoose 更新 MongoDB 文档时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 01:34:45 24 4
gpt4 key购买 nike

我在更新文档时遇到问题。当我使用 findByIdAndUpdate 更新文档时,它会更新,但这不是我想要的,因为我真的不想删除密码和其他内容。我真的不知道为什么密码、userCreated 和 userModified 被删除,因为 findByIdAndUpdate 使用 $set。

请求正文:

{
"userProfile": {
"username": "test",
"email": "test2",
"firstName": "Test3",
"lastName": "Test4"
}
}

原始文件:

{
"_id" : ObjectId("5c77b064cecb8c2eacaacdb4"),
"userProfile" : {
"firstName" : "John",
"lastName" : "Doe",
"userCreated" : "1551347812064",
"userModified" : "1551347812064",
"username" : "johndoe",
"email" : "john.doe@gmail.com",
"password" : "$2a$10$qkRYXk4fgFWJ3dVaYDMQM.4TzLIKUz8axf/lJU8HihvQIC6fVj7Jm"
},
"userSheets" : [ ],
"userTemplates" : [ ],
"__v" : 0
}

我想要什么:

{
"_id" : ObjectId("5c77b064cecb8c2eacaacdb4"),
"userProfile" : {
"firstName" : "UpdatedName",
"lastName" : "UpdatedLastname",
"userCreated" : "1551347812064",
"userModified" : "1551347812064",
"username" : "updatedUsername",
"email" : "updated.email@gmail.com",
"password" : "$2a$10$qkRYXk4fgFWJ3dVaYDMQM.4TzLIKUz8axf/lJU8HihvQIC6fVj7Jm"
},
"userSheets" : [ ],
"userTemplates" : [ ],
"__v" : 0
}

我得到了什么:

{
"_id" : ObjectId("5c77b064cecb8c2eacaacdb4"),
"userProfile" : {
"firstName" : "UpdatedName",
"lastName" : "UpdatedLastname",
"username" : "updatedUsername",
"email" : "updated.email@gmail.com"
},
"userSheets" : [ ],
"userTemplates" : [ ],
"__v" : 0
}

我不知道这里发生了什么,但在 userProfile 中只保留了我根据请求获得的 key 。

这是我在 mongoose 中使用的两个文件和代码:

用户.js

module.exports.updateUser = function(id, updatedUser, callback) {
var options = {
new: true
};
User.findByIdAndUpdate(id, updatedUser, options, (err, userAll) => {
if(err) throw err;
const user = {
success: true,
userProfile: userAll.userProfile
};
callback(null, user);
});
}

用户.js

router.route('/profile')
// Send user account
.get(passport.authenticate('jwt', {session: false}), (req, res) => {
res.json({user: req.user});
})
// Updates user account
.put(passport.authenticate('jwt', {session: false}), (req, res) => {
const username = req.body.userProfile.username;
const email = req.body.userProfile.email;
const id = req.user._id;

User.areUsernameAndEmailAvailable(username, email, id, (err, isAvailable) => {
if(err) throw err;
if(isAvailable.success) {
// If username and email are available, if updates user profile
const updatedUser = req.body;
User.updateUser(id, updatedUser, (err, updatedUserAndSuccess) => {
if(err) throw err;
res.json(updatedUserAndSuccess);
});
} else {
res.json(isAvailable);
}
});
})

架构:

const UserSchema = Schema({
userProfile: {
username: { type: String, require: true },
email: { type: String, require: true },
firstName: { type: String, default: '' },
lastName: { type: String, default: '' },
userCreated: { type: String, default: Date.now() },
userModified: { type: String, default: Date.now() },
password: { type: String, require: true }
},
userSheets: [
{
sheetName: { type: String },
sheetDescription: { type: String, default: '' },
sheetCreated: { type: Date, default: Date.now() },
sheetModified: { type: String, default: Date.now() },
status: { type: String, default: 'Not started yet' },
statusChanged: { type: Date, default: Date.now() },
sheetContent: {
itemType: { type: String },
itemRow: { type: Number },
itemColumn: { type: Number },
itemContent: { type: String, default: '' },
itemDatatype: { type: String },
}
}
],
userTemplates: [
{
templateName: { type: String },
templateDescription: { type: String, default: '' },
templateCreated: { type: Date, default: Date.now() },
templateModified: { type: Date, default: Date.now() },
templateContent: [
{
templateItemType: { type: String },
templateItemRow: { type: Number },
templateItemColumn: { type: Number },
templateItemContent: { type: String },
templateItemDatatype: { type: String },
}
]
}
]
});

Node :10.15.0蒙戈数据库:4.0.5 Mongoose :5.4.2 express :4.16.4

最佳答案

您要替换整个 userProfile 对象,您只需更新您想要的 key 。

考虑一下这是否是你的 body

body = {
"userProfile": {
"username": "test",
"email": "test2",
"firstName": "Test3",
"lastName": "Test4"
}
}

你应该传递这样的东西

User.findByIdAndUpdate(id, body.updatedUser)

关于node.js - 使用 mongoose 更新 MongoDB 文档时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54923809/

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