I'm writing a little planning poker tool in Meteor; first time working with meteor and json document stores. I have a Collection for user stories, along with the users assigned to that story and ratings from those users.
Initially I'll do some inserts like this:
Stories.insert({'story': stories[i], 'assignees': users, 'ratings': []});
... and one entry might look like this:
{
'story': 'some story',
'assignees': ['Bob', 'Joe'],
'ratings': []
}
Then, once a user has entered their rating for that story, it should be updated to look something like this:
{
'story': 'some story',
'assignees': ['Bob', 'Joe'],
'ratings': [{'Bob': 2}, {'Joe': 5}] // maybe a better way to do this?
}
I'm having trouble writing the line to make that happen. What I have now is:
var users = ['Bob', 'Joe'];
for (var i = 0; i < users.length; i++) {
var user = users[i];
var rating = {user: 1};
Stories.update({}, {$push: {'ratings': rating}});
}
Unfortunately, the result is something like:
{
'story': 'some story',
'assignees': ['Bob', 'Joe'],
'ratings': [{user: 1}]
}
That is, only one object in the ratings array, and not even of the correct key (plain user instead of something like Bob). This may have something to do with JS hoisting and object instantiation being tricky. Any insight is appreciated.
ratinghas been "pushed" onto the array. Are you actually asking how to "push"{ "user1": 1 }and{ "user2": 2 }onto the array with one statement?