我对onTorchModeChanged(0, false)
onTorchModeChanged(0, false)
onTorchModeUnavailable(0)
onTorchModeUnavailable(0)
NODEJS
框架很陌生。我需要更新下面显示的hapi
子文档
MongoDB
这是我的用户架构
我需要更新 {
"_id" : ObjectId("5bd1a22819e3e84e9dcae197"),
"userPrivacy" : {
"bio" : {
"aboutMe" : "only_me"
},
"userGallery" : {
"album" : "connections",
"images" : "connections",
"videos" : "connections"
}
},
"following" : [],
"followers" : [],
"invites" : [],
"referrals" : [],
"userId" : ObjectId("5bd1a22819e3e84e9dcae196")
}
userPrivacy
我已经为此编写了控制器:
( "bio" ,"userGallery" )
和数据库操作通过以下方式完成:
exports.updatePrivacyDefaults = {
description: 'update default privacy ',
tags: ['api'],
auth: {
mode: 'try',
strategy: 'session'
},
validate: {
payload: {
about: Joi.string().required().allow(''),
album: Joi.string().required().allow(''),
image: Joi.string().required().allow(''),
video: Joi.string().required().allow(''),
},
failAction: (request, h, error) => {
request.yar.flash('error', error.details[0].message);
return h.redirect('/settings').takeover();
}
},
handler: async (request, h) => {
try {
if (request.auth.isAuthenticated) {
// let userDetails = request.auth.credentials;
let incomingData = {
aboutMe: request.payload.about,
album: request.payload.album,
images: request.payload.image,
videos: request.payload.video,
};
try {
if (incomingData) {
console.log('data to update = ', incomingData);
let settingsUpdate = await settingHelper.updatePrivacySettings(request.auth.credentials._id, incomingData);
if (settingsUpdate.statusCode === 200) {
let UpdateData = settingsUpdate.userData;
request.yar.flash('success', UpdateData.message);
return h.redirect('/settings/privacy_settings');
}
// return h.view('setting/privacy_settings',{ user: userDetails});
}
} catch (error) {
logger.error(error);
request.yar.flash('error', error.message);
return h.redirect('/lounge');
}
// return h.view('setting/privacy_settings',{ user: userDetails});
}
else {
return h.redirect('/login');
}
} catch (error) {
logger.error(error);
}
}
};
我知道将无法正常工作,因为我正在尝试更新文档中的文档 有人,请帮我解决。我花了将近2天的时间来解决它,但没有结果
答案 0 :(得分:0)
Mongo
不知道如何处理incomingData
。您需要指定要更新的字段。在这种情况下,您可以执行以下操作:
let updatedPrivacyData = await UserDetails.findOneAndUpdate({
{ userId : user_id }, // query
{ userPrivacy.bio.aboutMe : incomingData.aboutMe, // data to update
userGallery.album : incomingData.album,
userGallery.images : incomingData.image,
userGallery.videos : incomingData.video }
});
另请参见mongoose api docs for findOneAndUpdate
您绝对应该研究使用模式: