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.
meteor:autopublishworking? Or how do you expect those documents to be populated to client? - Styxfind()andfindOne()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 accessCardsetsbefore 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