7

由于未找到用户,以下服务器方法引发重复键错误。一个典型的查询是:{services: {facebook: {id: 'XXXX'}}}

Meteor.methods
  getUser: (query, data = {}) ->
    user = Meteor.users.findOne(query)
    return user if user?
    user = _.extend(data, query)
    user._id = Meteor.users.insert user
    return user

据我了解,服务器方法可以访问集合中的所有文档,那么为什么找不到用户但由于重复的 facebook id 而插入失败?

这在我的 osx 开发环境中完美运行,但在我的 ubuntu 服务器(捆绑)上失败并使用 NODE_ENV=production 运行。

这是日志输出:

data:    { services: { facebook: { id: 'xxxx' } } } (the query provided to getUser)
data:    undefined (the result of findOne)
data:    Exception while invoking method 'getUser' MongoError: E11000 duplicate key error index: thunderstruck.users.$services.facebook.id_1  dup key: { : "xxxx" }
data:        at Db.wrap (/home/jeremy/deploy/thunderstruck/releases/20121105202012/bundle/server/node_modules/mongodb/lib/mongodb/db.js:1904:11)
data:        at null.<anonymous> (/home/jeremy/deploy/thunderstruck/releases/20121105202012/bundle/server/node_modules/mongodb/lib/mongodb/collection.js:320:26)
data:        at g (events.js:192:14)
data:        at EventEmitter.emit (events.js:126:20)
data:        at Db._callHandler (/home/jeremy/deploy/thunderstruck/releases/20121105202012/bundle/server/node_modules/mongodb/lib/mongodb/db.js:1439:25)
data:        at Server.connect.connectionPool.on.server._serverState (/home/jeremy/deploy/thunderstruck/releases/20121105202012/bundle/server/node_modules/mongodb/lib/mongodb/connection/server.js:425:30)
data:        at MongoReply.parseBody (/home/jeremy/deploy/thunderstruck/releases/20121105202012/bundle/server/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:127:5)
data:        at Server.connect.connectionPool.on.server._serverState (/home/jeremy/deploy/thunderstruck/releases/20121105202012/bundle/server/node_modules/mongodb/lib/mongodb/connection/server.js:384:22)
data:        at EventEmitter.emit (events.js:96:17)
data:        at _connect (/home/jeremy/deploy/thunderstruck/releases/20121105202012/bundle/server/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:136:13)
4

1 回答 1

14

这些是不同的 MongoDB 查询,您肯定想要切换到的点样式。请参阅Mongo Dot Notation文档。

Meteor.users.find({"services.facebook.id": "foo"})

将返回具有您正在查找的嵌入属性的任何文档 value foo

Meteor.users.find({services: {facebook: {id: "foo"}}})

仅匹配具有结构的文档。如果嵌入的 facebook 文档有其他字段,它将不匹配。您的生产数据库中的文档可能有更多字段,因此您没有得到匹配。

于 2012-11-09T17:02:29.260 回答