CodyBugstein
CodyBugstein

Reputation: 23372

mongoose findById works when I use a string literal but not when I reference a property of an Object

I'm having a really strange problem in Mongoose.

This line correctly finds the Round:

models.Round.findById("555ec731385b4d604356d8e5", function(err, roundref){
            console.log(roundref);
            ....

This line DOES NOT

models.Round.findById(result.round, function(err, roundref){
            console.log(roundref);

I've console logged result and it clearly is an object containing the property round:

{round: "555ec731385b4d604356d8e5", selection: 1, time: 20}

Why won't findById work without a literal?

Upvotes: 2

Views: 80

Answers (1)

victorkt
victorkt

Reputation: 14572

If result is a JSON string, calling .round would return undefined.

Try converting the JSON to a javascript object first:

result = JSON.parse(result);
models.Round.findById(result.round, function(err, roundref){
        console.log(roundref);

Upvotes: 2

Related Questions