javascript - Meteor: Update multiple entries in a database by their id's - Stack Overflow

I need help with the line for meteor that updates multiple entries in the database.The first Entries.

I need help with the line for meteor that updates multiple entries in the database. The first Entries.update below does not work I believe because meteor now requires you to update by id.

'click #draw': ->
      winner = _.shuffle(Entries.find(winner: {$ne: true}).fetch())[0]
      if winner
        Entries.update({recent: true}, {$set: {recent: false}}, {multi: true})
        Entries.update(winner._id, $set: {winner: true, recent: true})
  Template.entry.winner_class = ->
    if this.recent then 'highlight' else ''

So I tried to change to the below code. However, it does not work properly since it appears as it only changes one id (the first one).

'click #draw': ->
  winner = _.shuffle(Entries.find(winner: {$ne: true}).fetch())[0]
  recent_winner = Entries.find(recent: true).fetch()
  if winner
    Entries.update(recent_winner._id, {$set: {recent: false}}, {multi: true})
    Entries.update(winner._id, $set: {winner: true, recent: true})
Template.entry.winner_class = ->
  if this.recent then 'highlight' else ''

Any help would be appreciated.

I need help with the line for meteor that updates multiple entries in the database. The first Entries.update below does not work I believe because meteor now requires you to update by id.

'click #draw': ->
      winner = _.shuffle(Entries.find(winner: {$ne: true}).fetch())[0]
      if winner
        Entries.update({recent: true}, {$set: {recent: false}}, {multi: true})
        Entries.update(winner._id, $set: {winner: true, recent: true})
  Template.entry.winner_class = ->
    if this.recent then 'highlight' else ''

So I tried to change to the below code. However, it does not work properly since it appears as it only changes one id (the first one).

'click #draw': ->
  winner = _.shuffle(Entries.find(winner: {$ne: true}).fetch())[0]
  recent_winner = Entries.find(recent: true).fetch()
  if winner
    Entries.update(recent_winner._id, {$set: {recent: false}}, {multi: true})
    Entries.update(winner._id, $set: {winner: true, recent: true})
Template.entry.winner_class = ->
  if this.recent then 'highlight' else ''

Any help would be appreciated.

Share Improve this question asked May 20, 2014 at 3:02 Sean LSean L 8271 gold badge11 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You will need to modify multiple docs at once via Meteor.methods. From the documentation:

The behavior of update differs depending on whether it is called by trusted or untrusted code. Trusted code includes server code and method code. Untrusted code includes client-side code such as event handlers and a browser's JavaScript console.

Trusted code can modify multiple documents at once by setting multi to true, and can use an arbitrary Mongo selector to find the documents to modify. It bypasses any access control rules set up by allow and deny. The number of affected documents will be returned from the update call if you don't pass a callback.

Untrusted code can only modify a single document at once, specified by its _id. The modification is allowed only after checking any applicable allow and deny rules. The number of affected documents will be returned to the callback. Untrusted code cannot perform upserts, except in insecure mode.

EDIT:

By way of example, a method call might look something like this:

"click #draw": function(){
  var winner = _.shuffle(Entries.find({winner: {$ne: true}}).fetch())[0];
  if (!!winner){
    Meteor.call(
      "drawWinner", //an arbitrary method name of your choosing
      winner, // passing it your winner
      function(error, result){ // an optional async callback
        if (error){
          // handle error if error from method
        } else {
          // handle any return object from method
        }
      }
    );
  }
}

And then in your method call, which might be placed in a shared directory such as 'lib' or in a server side-only directory (see the Meteor documentation for more on this point):

Meteor.methods({
  "drawWinner": function(winner){
    Entries.update({recent: true}, {$set: {recent: false}}, {multi: true});
    Entries.update(winner._id, {$set: {winner: true, recent: true}});
    return winner; //or the like
  }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744227456a4564079.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信