3

尝试同时学习 Meteor 框架和 coffeescript/node。一直在开发一个使用 onloadend 的简单文件上传程序。当调用 FileReader onloadend 事件函数时,我会尝试确定文件是否已经存在,如果存在,我会使用新的文件数据和版本进行更新。

该代码适用于插入但不适用于更新。有人可以帮忙吗?我已经发布到流星谈话没有答案,因为我怀疑它是周末(当我做我的大部分实验时)。

代码片段...

file_reader.onloadend = ((file_event) ->
     (event) ->
        f_filename = escape file_event.name
        version = 0
        f_record = null

        f_record = doc_repo.findOne { name: f_filename }

        if f_record && f_record.name
          doc_repo.update
            name: f_filename
            ,
            $set:
              version: 10
        else
          doc_repo.insert
            name: f_filename
            data: event.target.result
            version: 0
    )(file_obj)

错误

Exception while invoking method '/documents/update' TypeError: Cannot read property 'toBSON' of undefined
    at Function.calculateObjectSize (/usr/local/meteor/lib/node_modules/mongodb/node_modules/bson/lib/bson/bson.js:210:12)
    at BSON.calculateObjectSize (/usr/local/meteor/lib/node_modules/mongodb/node_modules/bson/lib/bson/bson.js:1463:15)
    at UpdateCommand.toBinary (/usr/local/meteor/lib/node_modules/mongodb/lib/mongodb/commands/update_command.js:67:20)
    at Connection.write (/usr/local/meteor/lib/node_modules/mongodb/lib/mongodb/connection/connection.js:138:40)
    at __executeInsertCommand (/usr/local/meteor/lib/node_modules/mongodb/lib/mongodb/db.js:1837:14)
    at Db._executeInsertCommand (/usr/local/meteor/lib/node_modules/mongodb/lib/mongodb/db.js:1912:7)
    at Collection.update (/usr/local/meteor/lib/node_modules/mongodb/lib/mongodb/collection.js:445:13)
    at app/packages/mongo-livedata/mongo_driver.js:178:16
    at Db.collection (/usr/local/meteor/lib/node_modules/mongodb/lib/mongodb/db.js:507:44)
    at _Mongo._withCollection (app/packages/mongo-livedata/mongo_driver.js:51:13)  
4

1 回答 1

2

看起来 Mongo 没有获得它需要进行更新的第二个参数。因此,在常规 JavaScript 中,它期望这样:

collection.update({..selector..}, { .. modifier });

所以我会尝试在修饰符对象周围放置一些大括号,如下所示:

doc_repo.update
  name: f_filename,
  {
    $set:
      version: 10
  }
于 2013-02-27T22:58:28.117 回答