Find() works but findOne() does not


#1

I’m not sure what to try to troubleshoot this:

if(UserApp) {
    result = UserApp.findOne( {owner: Meteor.userId(), context: context, panel: this.direction} );
    if(result) {
        console.log('spanel.getApp:template=' + JSON.stringify(result) );

When running this as findOne, the if(result) is always false. When I run it with find, result is no longer empty and the stringified output of result has the data I am looking for.

I don’t understand why findOne is returning nothing? What can I try to troubleshoot this? I realize that findOne should return a cursor, while the other returns a single object, but it’s not even (apparently) returning a single object.


#2

find() returns a cursor even if it has no results. So it is not undefined.
Try if (find().count() === 0) { ... }


#3

There’s probably something else going on. You should definitely look at all the entries that are on the client, something like this:

// picking only relevant props
var items = _.pick(UserApp.find().fetch(), ['owner', 'context', 'panel']);
// console.log(items);
var selector = {owner: Meteor.userId(), context: context, panel: this.direction};
// console.log(selector);
var justOne = _.findWhere(items, selector);

That should provide a little insight at least into what’s happening there.


#4

Cool troubleshooting tip. So far I haven’t figured it out, but at least I have something to test.

var items = _.pick(UserApp.find().fetch(), ['owner', 'context', 'panel']);
console.log(items);

Is returning empty. There are certainly records in the database though, and I can confirm that by running db.userapp.find() in mongo. So it feels like my Publish/Subscribe isn’t working or something.


#5

Alright, I thought that you’d already tried the simplest of all “debug my Meteor collections client-side” tricks, which is to open the console and just type:

UserApp.find().fetch()

or

UserApp.findOne()

If you get nothing back, then you know your pub/sub isn’t working and you’ll have to check what you’re actually publishing and whether you’re actually subscribing in the right place(s).


#6

_.pick wanna an Object as first argument but you pass an Array! see: cursor.fetch()
And note that findOne is simply find(selector, {limit: 1}).fetch()[0], see: collection.findOne([selector], [options])


#7
var items = _UserApp.find().map(function(doc) {
    _.pick(doc, 'owner', 'context', 'panel');
});

#8

Whoopsie daisy, thanks for catching that one. I was so sure that you could use _.pick with an array and it would just understand that you wanted it to apply that to every item in the collection. But that’s not how it works!

In your code there you’re missing a return, though. Corrected version:

var items = UserApp.find().map(function(doc) { return _.pick(doc, 'owner', 'context', 'panel'); });

Sorry for that mistake on my part, hope it didn’t mess too much with @alfreema :blush:


#9

Well I never got _.pick to return an object but findOne started working (I am not 100% sure what I did to get it working but I suspect it was one of the gyrations of messing with subscribe/publish). I was going to post that _.pick wasn’t working for me but felt dumb enough for not using the console to test subscribe/publish, so I figured I was just using it wrong. LOL


#10

Oh, it is because of 2 years of CoffeeScript only)