Closed jdmcpeek closed 9 years ago
In player.js:
var plot_model = require('./plot');
var plot_schema = plot_model.schema;
var example = new plot_model();
console.log(example);
// returns { _id: 54752be0c649cd898d1f261b, beans: [] }
All good.
'schema' variable could be leaking through module.exports
Test.js: where does Steven's donation go?
plot.js:
plot_schema.methods.plant = function(beans){
for(var i=0; i<beans.length; i++){
this.beans.push(beans[i]);
}
};
What does 'this' describe here? The schema? Another thing: We pass the argument 'beans' into this fn, but what if 'beans' is actually a single object? For example, function donate_to_plot passes a single card into it.
Yup, function plant wasn't working because it expected an array and was getting a singular object.
We need to be sure we're passing the write data types into our methods.
Javascript gotcha: typeof Array === typeof Object // returns true.
Use Array.isArray to distinguish between objects and arrays.
@jdmcpeek - I'll try to be more explicit about what is expected from my methods. FYI if I use the plural of a single object I probably mean for it to be an array. Can you "overload" functions in javascript - not in the conventional way of course because javascript isn't "typed"?
so, now that we have the plot issue solved -> returning undefined because we weren't passing parameters properly.
but what is the deal with generate deck returning whatever a "full object" is?
check out the commit notes ee588f5000e9af82509d34eb4e76a63712c45e47. There's some weird behavior involving arrays happening...
can confirm - odd behavior does also affect player[i].plots but code remains functional.
@jdmcpeek - this was such a weird issue. I got it solved but best to check out the commit referenced in the PR.
Working down the chain dependences. In plot.js:
var model = mongoose.model('Plot', schema);
var example = new model();
console.log(example); // returns { _id: 54752a40ab970e658d4e336c, beans: [] }
All good.