Troubleshooting a Query in my Javascript code?

Not sure how I’ve gotten this far, but I have a query that I run based on an event. I ran it last night, and console logged the results based on the query , which was working.

Now I’ve moved it to a new location in my code, but I’m console logging each step, and I’m getting to the query, but getting nothing back, or so it would seem.

My issue, is that I’m using .fetch() in order to get an object back so I can manipulate the data in Javascript.

When I run the query in meteor mongo shell, it won’t run, and gives an error with .fetch() on it. If I remove .fetch() however, it runs fine and gives data back.

I had the same result last night where it would error in the shell, but give data back in code, so I’m guessing that the shell and code don’t always work the same.

Anyway, I’m wondering if there are any ways to troubleshoot the query in code? Any techniques that I could learn, or libraries I might use.

Thanks,

What error do you get?

Can we see the query?

Questions.find({ category: qCat }).fetch();

Where qCat is a variable value pulled from user input.

As I said, yesterday this was working, but today, nothing now that I’ve moved it.

The difference was that yesterday I filled qCat with the actual value “Family”, and had it triggered off of a different button click event, but I’m checking the value of qCat just before it’s put in the query with console.log, and it shows to be “Family” as it should for my testing.

I don’t get an error. I just don’t get any values returned from the query. I get an empty array object. I know, that there should be 7 documents returned though.

EDIT: Sorry, you meant what error in the Shell.

Let me try to create it again in a minute, and I"ll post it.

Could there be leading/trailing whitespace you’ve not noticed?

Possibly, @robfallows, I’ll check that in my data, but I’m puling that value from another collection to fill a select option, and I don’t believe I have any whitespace on any of them.

meteor:PRIMARY> db.questions.find({ category: "Family" }).fetch(); 2017-02-15T12:54:49.599-0600 E QUERY [thread1] TypeError: db.questions.find(...).fetch is not a function : @(shell):1:1

@robfallows here’s that error.

Again running the query as
db.questions.find({ category: "Family" }) gives what I expect to get back.

I did check as well, and no leading or trailing spaces.

That’s a mongo shell command not a meteor command, so fetch isn’t available as a method of find.

I missed that on your original post!

You can use.pretty() to make the find more readable.

Maybe try the meteor shell instead?

I am using meteor shell, In my project root I run meteor mongo then query from there for testing my queries and aggregations.

As I said, last night, from javascript I found that my

Questions.find({ category: "Family" }).fetch();

worked fine, but moving it to a different location in the code I only get back an empty array object. I’m intentionally using it to get an object as I need to use one of the fields to pseudo randomize before running another query to present data in the UI.

But meteor mongo isn’t the meteor shell. That’s “meteor shell”.

(On a phone - can’t get backticks.)

So, I’m not following, and forgive me on this.

I suppose there is a command meteor mongo, and a separate one for meteor shell?

I tried meteor shell with a message to use meteor --help.

So, I’m obviously missing something.

So just to hop in:

meteor shell gives you shell access to the Meteor process that is currently running. You are able to execute Meteor server-side code there, in Meteor syntax:

Questions.find().fetch();

meteor mongo gives you access to the mongo instance that is running. You are able to execute Mongo queries there, in Mongo syntax:

db.questions.find();

In looking at the actual issue you are trying to troubleshoot, I’d wager that you aren’t subscribed to the data (or at least not in the same way) in the place where you are trying to move that query as where it was originally.

If you are able to still test both locations, I’d try the following query:

console.log(Questions.find().count());

My guess is that in the old location, you are going to see a count of the documents you are subscribed to, and that in the new location, you are going to see a 0 count, indicating that you are missing a sub.

Ok, so never knew about meteor shell oil now. Wow! So sorry for the confusion.

I just tried my Query in meteor shell, but get the error

repl:1 Questions.find({ ^ ReferenceError: Questions is not defined at repl:1:-61 at packages/shell-server/shell-server.js:443:25 at /Users/brimcgon/.meteor/packages/promise/.0.8.8.1q6uhbq++os+web.browser+web.cordova/npm/node_modules/meteor-promise/fiber_pool.js:32:39

So, I’m obviously still doing something wrong.

EDIT

And both places where I’m calling the code from are in the same Template event object, just for different events. I don’t think that would be the issue, since I subscribe and import at the top of that file. I’ll try it though just to be sure.

If you are using imports, you’ll have to do something like:

import Questions from '/imports/lib/collections/questions';

I am, using imports and pub/sub. But I have narrowed it down. My variable I’m using as criteria for category doesn’t appear to be giving the same value as if I just give it a string like “Family”. But when I look at the “Family” value in the categories collection it is spelled the same, with the same capitalization, and no leading or trailing spaces…I don’t understand why it’s not seeing those as the same.

Ok, again I didn’t realize you meant in meteor shell. I’m just super slow today…geez.

I imported Questions now in meteor shell, and when I try

Questions.find({}).fetch(); or Questions.find().fetch();

I get

Questions.find().fetch(); ^ TypeError: Questions.find is not a function

Ok, figured out one of my problems…I need to look into this elsewhere, and sorry for all the trouble, but seriously, thank you for all the help today.

I have my categories selection in the UI as a Multiple Select box. I changed it just now to see if it was something about that, to a normal drop-down style select where I can only select one thing at a time, and now it works using the variable with no issue. So the multi-select must be adding something to my value, or perhaps it sees it as an array vs. a single string…just need to figure that out on my own.

You guys rule, as always!

2 Likes

@vigorwebsolutions : Thanks for jumping in there Sean. I struggle trying to answer on a mobile phone. You said it all :smile:

1 Like