1

我在流星应用程序中有一个任务集合。我想将任务从“待办事项”推到“做”和“完成”statuses

我已经将它们放在列表中,并在 DOM 中使用它们的 id

创建了一个meteor方法来更新一个Task,叫做method,没用。什么都没发生,任务不会更新。

所以,我尝试直接调用 Task.update 方法

 Meteor.methods({
    create : function(json){Task.insert(json);},
    update : function(id, conditions){Task.update(id, {$set : conditions});}
  });

Template.todo.events({
    'click .submit' : function(){
      console.log("event called",this._id);
      //Meteor.call('update',(this._id, {'status':'doing'}));
      Task.update(this._id, {'status':'doing'});
    }
  });

当 Task.update(this._id, {'status':'doing'});

叫做。集合中的描述字段为空,仅更新状态字段。

Task.update 之前

{ "_id" : "er6Rqvdj8P373cPM5", "status" : "doing" , "description" : "work out and eat green chillies"}

Task.update("er6Rqvdj8P373cPM5",{"status":"done"})

{ "_id" : "er6Rqvdj8P373cPM5", "status" : "done" }

打电话时update : function(id, conditions){Task.update(id, {$set : conditions});}

我在流星日志中收到以下错误

调用方法 'update' MongoError 时出现异常:指定的修饰符无效:$set

Exception while invoking method 'update' MongoError: Invalid modifier specified: $set
    at Object.Future.wait (/Users/otto/.meteor/tools/275efdf2db/lib/node_modules/fibers/future.js:322:15)
    at _Mongo.update (app/packages/mongo-livedata/mongo_driver.js:269:12)
    at _.each.Meteor.Collection.(anonymous function) [as update] (app/packages/mongo-livedata/collection.js:354:32)
    at Meteor.methods.update (app/alacrity.js:39:45)
    at _.extend.protocol_handlers.method.exception (app/packages/livedata/livedata_server.js:593:30)
    at _.extend.withValue (app/packages/meteor/dynamics_nodejs.js:31:17)
    at app/packages/livedata/livedata_server.js:592:46
    at _.extend.withValue (app/packages/meteor/dynamics_nodejs.js:31:17)
    at _.extend.protocol_handlers.method (app/packages/livedata/livedata_server.js:591:37)
    at _.extend.processMessage.processNext (app/packages/livedata/livedata_server.js:484:43)
    - - - - -
    at Object.exports.toError (/Users/otto/.meteor/packages/mongo-livedata/405d21f01568c045554ee5b99d2bea88a5249feb/.npm/node_modules/mongodb/lib/mongodb/utils.js:108:11)
    at null.<anonymous> (/Users/otto/.meteor/packages/mongo-livedata/405d21f01568c045554ee5b99d2bea88a5249feb/.npm/node_modules/mongodb/lib/mongodb/collection.js:477:24)
    at g (events.js:192:14)
    at EventEmitter.emit (events.js:126:20)
    at Server.Base._callHandler (/Users/otto/.meteor/packages/mongo-livedata/405d21f01568c045554ee5b99d2bea88a5249feb/.npm/node_modules/mongodb/lib/mongodb/connection/base.js:130:25)
    at Server.connect.connectionPool.on.server._serverState (/Users/otto/.meteor/packages/mongo-livedata/405d21f01568c045554ee5b99d2bea88a5249feb/.npm/node_modules/mongodb/lib/mongodb/connection/server.js:517:20)
    at MongoReply.parseBody (/Users/otto/.meteor/packages/mongo-livedata/405d21f01568c045554ee5b99d2bea88a5249feb/.npm/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:127:5)
    at Server.connect.connectionPool.on.server._serverState (/Users/otto/.meteor/packages/mongo-livedata/405d21f01568c045554ee5b99d2bea88a5249feb/.npm/node_modules/mongodb/lib/mongodb/connection/server.js:476:22)
    at EventEmitter.emit (events.js:96:17)
    at _connect (/Users/otto/.meteor/packages/mongo-livedata/405d21f01568c045554ee5b99d2bea88a5249feb/.npm/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:175:13)

虽然流星文档说:

// mark my first message as "important"
Messages.update(myMessages[0]._id, {$set: {important: true}});

http://docs.meteor.com/#meteor_user

4

2 回答 2

3

我猜你直到现在才发现错误,但我将这个发布给其他可能面临同样问题的人。

此行有额外的括号:

Meteor.call('update',(this._id, {'status':'doing'}));

它应该是这样的:

Meteor.call('update',this._id, {'status':'doing'});

如果您收到此消息:

MongoError: Invalid modifier specified: $set

问题可能在于 $set 旁边的内容。在上述情况下是“未定义的”。

今天发生在我身上:)

于 2013-06-21T12:48:27.000 回答
1

这取决于您使用的修饰符 & 可能会触发此错误。

基本上,如果您使用:

Task.update(this._id, {'status':'doing'});

它将文档设置this._id{status: 'doing'}

如果您使用$set它将仅设置指定的那些字段:

Task.update(this._id, { $set: {'status': 'doing'}});

您也不必使用方法,并且可以直接在客户端上执行。有关如何在mongodb 文档中使用 $set 的更多详细信息

于 2013-04-30T00:05:10.683 回答