This question already has an answer here:
这个问题在这里已有答案:
- mongodb javascript update 1 answer
mongodb javascript update 1 answer
My node js code,
我的节点是js代码,
exports.updateprofiledetails = function (req, res) {
console.log(req.body)
var params = req.body;
var item =
{
firstname:params.firstname,
lastname:params.lastname,
phone:params.phone,
street:params.street
};console.log(item)
var id = req.params.id;
var record= db.collection('profile');
record.updateOne({'_id':id},{$set:item},(err, result) => {
if (err){ return console.log(err)
}
if(result){
response = {status:'success',data:result};
} else{
response = {status:'fail',data:[]};
}
res.send(response);
});
};
From the above i give only 2(firstname,lastname) fields but then the remaining fields(phone.street) are assigned with null values.Can someone help me solve the problem.
从上面我只给出2(名字,姓氏)字段,但其余字段(phone.street)被赋予空值。有人帮我解决问题。
2 个解决方案
#1
1
You may like to check and then set value that is only set value which sent from front end like
您可能想检查然后设置仅从前端发送的设置值的值
exports.updateprofiledetails = function (req, res) {
console.log(req.body)
var params = req.body;
var item = { }
if(params.firstname)
item.firstname = params.firstname;
if(params.lastname)
item.lastname = params.lastname;
if(params.phone)
item.phone = params.phone;
if(params.street)
item.street = params.street;
console.log(item);
var id = req.params.id;
var record= db.collection('profile');
record.updateOne({'_id':id},{$set:item},(err, result) => {
if (err) {
return console.log(err)
}
if(result){
response = {status:'success',data:result};
} else{
response = {status:'fail',data:[]};
}
res.send(response);
});
};
#2
0
Everything is fine in your code.But you need to ensure the field phone ,street value exist in variable item.Because your only give firstname & lastname from params.If your not giving the value to phone & street (via params) please remove the field from item variable.
你的代码中的一切都很好。但是你需要确保现场电话,街道价值存在于变量项目中。因为你只给了params的名字和姓氏。如果你没有给电话和街道的价值(通过参数),请删除项变量的字段。
Try the below code and Let me know what its returns
尝试下面的代码,让我知道它的回报
exports.updateprofiledetails = function (req, res) {
console.log(req.body)
var params = req.body;
var item =
{
firstname:params.firstname,
lastname:params.lastname
};
console.log(item) // Ensure this variable have data
var id = req.params.id;
var record= db.collection('profile');
record.updateOne({'_id':id},{$set:item},(err, result) => {
if (err){ return console.log(err)
}
if(result){
response = {status:'success',data:result};
} else{
response = {status:'fail',data:[]};
}
res.send(response);
});
};
#1
1
You may like to check and then set value that is only set value which sent from front end like
您可能想检查然后设置仅从前端发送的设置值的值
exports.updateprofiledetails = function (req, res) {
console.log(req.body)
var params = req.body;
var item = { }
if(params.firstname)
item.firstname = params.firstname;
if(params.lastname)
item.lastname = params.lastname;
if(params.phone)
item.phone = params.phone;
if(params.street)
item.street = params.street;
console.log(item);
var id = req.params.id;
var record= db.collection('profile');
record.updateOne({'_id':id},{$set:item},(err, result) => {
if (err) {
return console.log(err)
}
if(result){
response = {status:'success',data:result};
} else{
response = {status:'fail',data:[]};
}
res.send(response);
});
};
#2
0
Everything is fine in your code.But you need to ensure the field phone ,street value exist in variable item.Because your only give firstname & lastname from params.If your not giving the value to phone & street (via params) please remove the field from item variable.
你的代码中的一切都很好。但是你需要确保现场电话,街道价值存在于变量项目中。因为你只给了params的名字和姓氏。如果你没有给电话和街道的价值(通过参数),请删除项变量的字段。
Try the below code and Let me know what its returns
尝试下面的代码,让我知道它的回报
exports.updateprofiledetails = function (req, res) {
console.log(req.body)
var params = req.body;
var item =
{
firstname:params.firstname,
lastname:params.lastname
};
console.log(item) // Ensure this variable have data
var id = req.params.id;
var record= db.collection('profile');
record.updateOne({'_id':id},{$set:item},(err, result) => {
if (err){ return console.log(err)
}
if(result){
response = {status:'success',data:result};
} else{
response = {status:'fail',data:[]};
}
res.send(response);
});
};