调用Foo.update(id,{conditions})时,Meteor.js会删除文档字段

时间:2013-04-29 11:56:46

标签: mongodb meteor

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

我在DOM中列出了他们的ID

创建了一个更新任务的流星方法,称为方法,不起作用。什么都没发生,任务不会更新。

所以,我尝试直接调用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

2 个答案:

答案 0 :(得分:3)

我猜你到目前为止已经发现了这个错误,但是我发布了这个可能面临同样问题的其他人。

这一行有额外的括号:

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

应该是这样的:

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

如果您收到此消息:

MongoError: Invalid modifier specified: $set

问题可能出在$ set旁边的问题上。在上面的情况中是&#34;未定义&#34;。

今天发生在我身上:)

答案 1 :(得分:1)

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

基本上如果您使用:

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

它会将this._id设置为{status: 'doing'}

如果您使用$set,则只会设置指定的字段:

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

您也不必使用方法&amp;可以直接在客户端上进行。有关如何在mongodb docs

使用$ set的更多详细信息