Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a meteor user on the server

Tags:

mongodb

meteor

The following server method is throwing duplicate key errors because the users are not being found. A typical query is: {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

It is my understanding that server methods have access to all documents in collections so why wouldn't the user be found but the insert fail due to a duplicate facebook id?

This works perfectly on my osx dev environment but fails on my ubuntu server (bundled) and running with NODE_ENV=production.

Here is the log output:

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)
like image 574
Jeremy Dunn Avatar asked Nov 05 '12 20:11

Jeremy Dunn


People also ask

What is the meteor user () function for?

userId() import { Meteor } from 'meteor/meteor' (accounts-base/accounts_common.js, line 410) Get the current user id, or null if no user is logged in.

How do I call Meteor method?

// Asynchronous call Meteor. call('foo', 1, 2, (error, result) => { ... }); If you do not pass a callback on the server, the method invocation will block until the method is complete. It will eventually return the return value of the method, or it will throw an exception if the method threw an exception.

What is Meteor API?

Meteor is a full-stack JavaScript platform for developing modern web and mobile applications. Meteor includes a key set of technologies for building connected-client reactive applications, a build tool, and a curated set of packages from the Node. js and general JavaScript community.

What is the name of the package that provides basic user accounts functionality?

`accounts-base` This package is the core of Meteor's developer-facing user accounts functionality.


1 Answers

Those are different MongoDB queries, and you definitely want the dotted-style that you switched to. See the Mongo Dot Notation documentation.

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

will return any document that has the embedded property you're looking for with value foo.

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

only matches documents with exactly that structure. If the embedded facebook document has other fields, it won't match. It's likely that the document in your production DB has more fields and so you weren't getting a match.

like image 104
debergalis Avatar answered Sep 25 '22 07:09

debergalis