admin管理员组文章数量:1253753
I have a collection that I need to update when the user presses a button. I just need to change one variable to another.
In the console, this line of code works:
db.users.update({username : "Jack"},{age : 13, username : "Jack"});
But when I put in this code:
Template.body.events({
'click #updateAge' = function() {
{
alert();
db.users.update({username : "Jack"},{age : 13, username : "Jack"});
}
}
})
into my JavaScript file for Meteor.js, it doesn't do anything at all (I don't get an error message, and I see the alert, but the update just doesn't work).
I've read through the Meteor Documentation on updating, but I just can't seem to get it to work. Does anybody know what I'm doing wrong here?
I have a collection that I need to update when the user presses a button. I just need to change one variable to another.
In the console, this line of code works:
db.users.update({username : "Jack"},{age : 13, username : "Jack"});
But when I put in this code:
Template.body.events({
'click #updateAge' = function() {
{
alert();
db.users.update({username : "Jack"},{age : 13, username : "Jack"});
}
}
})
into my JavaScript file for Meteor.js, it doesn't do anything at all (I don't get an error message, and I see the alert, but the update just doesn't work).
I've read through the Meteor Documentation on updating, but I just can't seem to get it to work. Does anybody know what I'm doing wrong here?
Share Improve this question edited Dec 16, 2017 at 23:13 Ian Wise asked Aug 1, 2014 at 23:16 Ian WiseIan Wise 7663 gold badges11 silver badges34 bronze badges 2- 1 Does just using users.update(...) work? There is a solution like this here: stackoverflow./questions/14016149/… – sha1 Commented Aug 1, 2014 at 23:27
- I think Mongo needs a selector, which is why I have my {username : "Jack"} but they are using the same thing I am... Unless, do you mean the Meteor tag in front of the users.update? – Ian Wise Commented Aug 1, 2014 at 23:38
3 Answers
Reset to default 11Found the problem.
Since I defined my database in my lib.js file
users = new Meteor.collection("users");
I don't need to put a db in front of the db.users.update({_id : "Jack"},{...})
. I also need to find the document using the given mongo _id
, not the identifier "username"
.
so the appropriate code would be
users.update({_id : "Jack"},{$set:{age : 13, username : "Jack"}});
In mongodb, you will have to use an update operator(for ex: $set
). Otherwise, your document will overwritten by the update object you are passing(I am not sure that's what you want). I think, its works same in meteor. So, you will have to do something like this:
Meteor.users.update({username : "Jack"},{$set: {age : 13}});
This might not be the problem as you've stated you do not get a error message but to be sure: have you already allowed the user to update documents in the user collection?
Something like:
(in collections/permissions.js)
user.allow({
update: function (userId) {
// the user must be logged in to allow updates
return (userId != null);
}
})
本文标签: javascripthow to update a Mongodb collection in meteorjsStack Overflow
版权声明:本文标题:javascript - how to update a Mongo.db collection in meteor.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740777127a2284462.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论