javascript - Meteor:通过 ID 更新数据库中的多个条目

标签 javascript meteor

我需要帮助更新数据库中多个条目的 meteor 行。我认为下面的第一个 Entries.update 不起作用,因为 meteor 现在需要您通过 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 ''

所以我试着改成下面的代码。但是,它无法正常工作,因为它看起来只更改了一个 ID(第一个)。

'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 ''

如有任何帮助,我们将不胜感激。

最佳答案

您将需要通过 Meteor.methods 一次修改多个文档。来自 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.

编辑:

例如,方法调用可能看起来像这样:

"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
        }
      }
    );
  }
}

然后在您的方法调用中,它可能被放置在共享目录中,例如“lib”或服务器端专用目录中(有关这一点的更多信息,请参阅 Meteor 文档):

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
  }
});

关于javascript - Meteor:通过 ID 更新数据库中的多个条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23750359/

相关文章:

javascript - 使用 promise 的链接方法

JavaScript 图像作为按钮 (onClick)

javascript - Angular 1.5 范围变量在 PouchDB 响应函数中不起作用

javascript - 将jquery修改应用于meteor站点

javascript - 为什么谷歌网站管理员工具看不到我网站的静态版本,而是动态版本的模板?

javascript - 如何在meteor中使用jQuery代码?

javascript - 如何检查其他数组中存在的值,反之亦然

javascript - 如何动态地将选项添加到正确位置的选择框?

javascript - 如何在 Meteor 模板中打印键和值?

javascript - Meteor 的混合原生组件