0
votes

I'm (obiously) a beginner, first post. I'm creating a flashcards project with Meteor. I have a db called "cardsets" that contains lots of documents that look similar to this:

{ "_id" : "asdfg00724", 
"setTitle" : "Les animaux", 
"vocab" : [ 
[ "the dog", "le chien" ],
[ "the cat", "le chat" ], 
[ "the rabbit", "le lapin" ], 
[ "the horse", "le cheval" ] ] }

What I'm trying to do is to access a single document from the collection and assign each field in the document to a variable of the same name, with the 'vocab' field being brought in as an array.

I have an /imports/api/cardsets.js that contains this:

import { Mongo } from 'meteor/mongo';
export const Cardsets = new Mongo.Collection('cardsets');

The /server/main.js includes this line:

import '../imports/api/cardsets.js';

And the /client/main.js file includes this line:

import { Cardsets } from '/imports/api/cardsets.js';

In the /client/main.js file, I am able to directly 'insert' new documents into the database, so I know the Cardfiles const is accessible in main.js:

Cardsets.insert( {language: "Chinese", course: "CHIN102"});

Works as expected. (This is only for testing). However, NOTHING I have done with the "findOne" has produced any results:

var asdf = Cardsets.findOne( { _id : "asdfg00724"});
console.log("asdf = "+asdf);

Produces "asdf = undefined" in the console.

And if I type "Cardsets.find({}).fetch()" in the browser console, I get an error: "Cardsets is not defined."

I've tried this as well:

Cardsets.findOne({ _id : "asdfg00724"}, function(err, document) {
     console.log(document.name);
     });

But this produces a huge error containing the text "Match error: Failed Match.OneOf, Match.Maybe or Match.Optional validation".

I realize that there's probably a very easy thing I'm missing, but after working on this for many days non-stop, I felt it's time to ask for help.

1
Do you have meteor:autopublish working? Or how do you expect those documents to be populated to client? - Styx
Yes, meteor list returns "autopublish 1.0.7". - Will Lehman
(1) find() and findOne() do not accept callbacks as parameters, hence your huge error. (2) your variables (including collections) will not be available in the console unless you're at a breakpoint inside a function where those variables are defined, (3) most likely you're trying to access Cardsets before the client has received that data from the server via pub-sub. Once you implement actual publications and subscriptions you'll be able to look at the .ready() state of subscriptions to know when your data is available. - Michel Floyd
Looking at everything you've got, this should work. Can you upload a reproduction to github? - coagmano
Thanks for the comments! I moved the findOne() statement to a later function and everything worked as expected. Apparently @MichelFloyd was right: the data hadn't had time to be loaded. I was also able to get all the keys assigned to variables, so basically my whole problem is solved. Now to figure out how to mark this question as answers/solved... - Will Lehman

1 Answers

0
votes
  1. find() and findOne() do not accept callbacks as parameters, hence your huge error.
  2. your variables (including collections) will not be available in the console unless you're at a breakpoint inside a function where those variables are defined,
  3. most likely you're trying to access Cardsets before the client has received that data from the server via pub-sub. Once you implement actual publications and subscriptions you'll be able to look at the .ready() state of subscriptions to know when your data is available.