[英]why does my node.js endpoint for saving data into array raises error while modifying data?
In my mongoose
schema I have the following model for storing users: 在我的mongoose
模式中,我具有以下用于存储用户的模型:
var UsersSchema = new Schema({
username: {type: String},
our_username: {type: String},
our_display_name: {type: String},
hashtags: {type: [String]}
}
I want to create an endpoint in node.js that allows user's to update (add/remove) those hashtags, my use case scenario is that when user logs in to my app, I show him all hashtags with a simple get
endpoint: 我想在node.js中创建一个端点,以允许用户更新(添加/删除)这些标签,我的用例场景是当用户登录到我的应用程序时,我用一个简单的get
端点向他显示所有标签:
usersRoutes.get('/:username/hashtags/', functions.validateRequestsGET, function(req, res){
var username = req.params.username;
var query;
if(username != undefined) {
query = User.find({}).where('username').equals(username).select('hashtags -_id');
}
query.exec(function(err, hashtags){
if(err) {
res.send(err);
return;
}
res.json(hashtags);
});
});
But how can I create an endpoint that allows user to add/remove hashtags? 但是,如何创建一个允许用户添加/删除主题标签的终结点? I tried with this: 我尝试了这个:
usersRoutes.post('/:username/', function (req, res) {
var username = req.params.username;
var hashtagsToAdd = req.body.hashtagsToAdd;
var hashtagsToRemove = req.body.hashtagsToRemove;
console.log(hashtagsToAdd);
console.log(hashtagsToRemove);
User.findOneAndUpdate(
{username: username},
{
$push: {"hashtags": hashtagsToAdd},
$pull: {"hashtags": hashtagsToRemove},
},
{safe: true, upsert: true},
function(err, user) {
if(err)
console.log(err);
return callback(err);
res.json(user);
}
);
});
and I use it with sending this: 我用它发送:
{"hashtagsToRemove":["food"], "hashtagsToAdd":["something"]}
to my endpoint: 到我的端点:
http://localhost:3000/users/randomUsername/
but it raises an error: 但这会引发错误:
message: 'Cannot update \'hashtags\' and \'hashtags\' at the same time'
how can I modify my endpoint so that it covers the possibility of adding/removing hashtags? 如何修改端点,使其涵盖添加/删除主题标签的可能性?
You're doing a $pull
and a $push
at the same time on MongoDB, which is not allowed. 您在MongoDB上同时执行$pull
和$push
,这是不允许的。
You have two possibilities to achieve what you want to do: 您有两种可能性来实现自己想要做的事情:
Option 1 选项1
findOne
) 获取用户(使用findOne
) user.hashtags
attribute 从user.hashtags
属性手动添加和删除#标签 hashtags
attribute using the $set
operator. 使用$set
运算符更新用户hashtags
属性。 Example: 例:
User.findOne({username: username}, function (err, user) {
user.hashtags = addToArray(user.hashtags, hashtagsToAdd);
user.hashtags = removeFromArray(user.hashtags, hashtagsToRemove);
user.save(function (err, updatedUser) {
console.log('yeepee');
});
});
addToArray
and removeFromArray
are left for exercise ;-) addToArray
和removeFromArray
留给练习;-)
Option 2 选项2
You can update the user in sequence: a first request with $push
and, in the callback, another with $pull
. 您可以依次更新用户:使用$push
的第一个请求,在回调中使用$pull
另一个请求。
Feature request 功能要求
There is an opened feature request: https://jira.mongodb.org/browse/SERVER-1050 But there is no plan to implement it. 有一个打开的功能请求: https : //jira.mongodb.org/browse/SERVER-1050但是没有实现它的计划。
Related issues: 相关问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.