Get _id instead of name in Collection.findOne()

I have following code in my router

Router.route('/guide/:_id', function () {
    this.render('guide_detail', {
        data: function () {
            var guide = Guides.findOne({_id: this.params._id});
            console.log("The cards of this guide are: ", guide.cards);
            var cards = Cards.find({_id : guide.cards }).fetch();
            console.log("Actually fetched cards: ", cards);
            return {
                guide: guide,
                cards: cards
            };
        }
    });
}, {name: 'guide_detail'});

When I console.log(guide.cards) what is a list of ObjectId’s I receive the name of these cards. This is how my guide schema looks like at the moment.

GuideSchema = new SimpleSchema({
    title: {
        type: String,
        label: "Title",
        max: 200
    },
    isPublic: {
        type: Boolean,
        label: "Public"
    },
    cards: {
        type: [String],
        minCount: 0,
        maxCount: 30,
        label: "Cards"

    ... and some other stuff

I want to render the template and give the guides and the card objects that belong to this guide. My approach was to find the guide I want to give to the template and then search for the cards of the guide according to guides.cards. Unfortunately this is not working properly and I wanted to ask you guys, how I am able to solve my problem. Is it somehow possible to receive the ID’s of the cards? Or is there even a better approach to do what I want to do?

In addition this is a card object from my database:

{ “_id” : ObjectId(“564c7fefef88adec793948f5”), “name” : “Leper Gnome”, “mana” : 1, “rarity” : “Common”, “dust” : 40, “cardClass” : “All”, “cardSet” : “Classic”, “cardType” : “Monster”, “imageUrl” : “/images/cards/LeperGnome.png”, “damage” : 2, “health” : 1 }

var cards = Cards.find({_id : {$in: guide.cards }});

you need $in to match something from array
and you probably dont want to fetch() in data context, as blaze is better with cursors than arrays

And many people use publish composite package to do joins between collections.
Also if you put guide_id in cards, you can pub/sub them easily without need to wait for guides subscription to be ready. And also that query will be there fully reactive.

1 Like

Hm it seems like this doesn’t work, this is what I receive in my developer console

Object { collection: Object, sorter: null, matcher: Object, _selectorId: undefined, skip: undefined, limit: undefined, fields: undefined, _projectionFn: LocalCollection._compileProjection/<(), _transform: null, reactive: true }

If I would use publish-composite will I be still able to query these guides and cards in my router or will these things be executed in server + client?

It works, if you use fetch() it will print correctly in console.
But for setting data context, use it without fetch() cause it is more performant for the Blaze engine

I am using the jade templating engine, does this relate to jade the same? This is the output Array [ ] when I use var cards = Cards.find({_id : {$in: guide.cards}}).fetch();. Maybe this is the case because this is the output of db.cards.find()

{ “_id” : ObjectId(“564c6f2395bc723f9df0353c”), “name” : “Leper Gnome”, “mana” : 1, “rarity” : “Common”, “dust” : 40, “cardClass” : “All”, “cardSet” : “Classic”, “cardType” : “Monster”, “imageUrl” : “/images/cards/LeperGnome.png”, “damage” : 2, “health” : 1 }
{ “_id” : ObjectId(“564c7fefef88adec793948f5”), “name” : “Leper Gnome”, “mana” : 1, “rarity” : “Common”, “dust” : 40, “cardClass” : “All”, “cardSet” : “Classic”, “cardType” : “Monster”, “imageUrl” : “/images/cards/LeperGnome.png”, “damage” : 2, “health” : 1 }

I have 2 objects for the same card because a player can have the exact same card two times in his deck. Maybe it searches for names, recognizes that there are to entries for the same name and then just releases an empty array :joy: I have no other explanation for this behaviour…

I have strange feeling about these ObjectId in _id, did you create cards outside of Meteor ?

Yes I created them manually in the meteor mongo console, is this bad?

than change their’s _id to strings as Meteor expects them.
Good luck

1 Like

Well seems like I have to write crud functions for my cards now… :smiley: Thank you for your help!