我想使用 AngularJS 和 NodeJS 更新我的 MongoDB。我想使用 id 进行更新。
我发现以下代码更新了 id 为 1 的对象。我想将 id 作为参数传递,然后更新对象
module.exports.updateUser = function (req, res) {
// get a user with ID of 1
User.findById(1, function(err, user) {
if (err) throw err;
// change the users location
user.auto = 'true';
// save the user
user.save(function(err) {
if (err) throw err;
console.log('User successfully updated!');
});
});
}
你能帮忙吗
最佳答案
您可以在请求中传递 ID,然后访问它
module.exports.updateUser = function (req, res) {
var id = req.body.id;
// get a user with ID of id
User.findById(id, function(err, user) {
if (err) throw err;
//update the object like you want
//save the object like you were doing
关于javascript - 使用nodejs和angularjs更新mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40866833/