【问题标题】:Cannot use 'in' operator to search for '_id' in无法使用“in”运算符搜索“_id”
【发布时间】:2013-01-15 01:23:00
【问题描述】:

我正在尝试使用 mongoose 和 express 获取现有用户文档,但我只得到这个:

/webroot/api.domain.com/production/node_modules/mongoose/lib/document.js:162
      if (obj && '_id' in obj) continue;
                          ^
TypeError: Cannot use 'in' operator to search for '_id' in Account
    at model.Document._buildDoc (/webroot/api.domain.com/production/node_modules/mongoose/lib/document.js:162:27)
    at model.Document (/webroot/api.domain.com/production/node_modules/mongoose/lib/document.js:67:20)
    at model.Model (/webroot/api.domain.com/production/node_modules/mongoose/lib/model.js:33:12)
    at new model (/webroot/api.domain.com/production/node_modules/mongoose/lib/model.js:1663:11)
    at Object.model (/webroot/api.domain.com/production/node_modules/mongoose/lib/model.js:1662:14)
    at Object.findOrCreateOneForTwitter (/webroot/api.domain.com/production/models/account.coffee:60:17)
    at module.exports.callback.error (/webroot/api.domain.com/production/endpoints/twitter.coffee:67:34)
    at Twitter.get (/webroot/api.domain.com/production/node_modules/ntwitter/lib/twitter.js:85:7)
    at passBackControl (/webroot/api.domain.com/production/node_modules/oauth/lib/oauth.js:361:11)
    at IncomingMessage.exports.OAuth._performSecureRequest.request.on.callbackCalled (/webroot/api.domain.com/production/node_modules/oauth/lib/oauth.js:380:9)

是否有任何已知的解决方法?谢谢。

【问题讨论】:

  • obj.hasOwnProperty("_id") 也会失败吗?
  • 您能否更新您的问题以包含发生这种情况的代码?

标签: javascript node.js express coffeescript mongoose


【解决方案1】:

这个错误不是 Mongoose 特有的;它会显示obj 是否不是一个对象,而是一个字符串,例如:

var obj = 'Account';
'id' in obj;
// TypeError: Cannot use 'in' operator to search for 'id' in Account

假设这不是 Mongoose 的问题,你会在这里寻找:

at Object.findOrCreateOneForTwitter 
    (/webroot/api.domain.com/production/models/account.coffee:60:17)

【讨论】:

  • 哦,谢谢,现在意识到我忘了require 的东西已经太晚了。
【解决方案2】:

顺便说一下,如果您尝试加载 AMD 模块并且使用 require 而不是 define 来定义模块,您会从 dojo 解析器中得到相同的错误。

【讨论】:

    猜你喜欢
    • 2015-02-19
    • 2013-04-05
    • 2013-06-10
    • 1970-01-01
    • 2016-04-06
    • 2016-03-03
    • 2018-05-02
    • 2018-05-17
    • 2019-10-10
    相关资源
    最近更新 更多