javascript - Mongoose, array in object -
update: problem seems related mongoose. please see update @ bottom.
in parent use following code pass choosenitem grandchild:
childcontexttypes: { foo: react.proptypes.object.isrequired, }, getchildcontext: function() { return { foo: this.state.choosendriver } }, choosendriver mongoose model looks this:
var schema = mongoose.schema; var driverschema = new mongoose.schema({ driver: string, age: string, cars: [{ type: schema.types.objectid, ref: 'car' }] }); module.exports = mongoose.model('driver', driverschema); this works fine , in grandchild can following:
contexttypes: { foo: react.proptypes.object.isrequired, }, //some other stuff... render: function(){ return ( <h1>{this.context.foo.name}</h1> ) }); and displays name of choosendriver.
i loop through cars , thought done this:
render: function(){ var cars = new array(this.context.foo.cars); var driverscars = cars.map(function(car,i){ return <li key={i}> {car} </li>; }); return ( <div> <h1>{this.context.foo.name}</h1> <ul>{driverscars}</ul> </div> ) } }); this returns drivers cars in string displayed on 1 single line. took in react developers tool , saw cars not defined array.
is maybe because choosendriver state , arrays not supported in manner?
the cars exists not array.
any ideas on how access cars array in example?
thank you!
update:
when looking @ this.context.foo.cars in react-tools see this:
cars: ->_proto_ :{..} 0: "4242594395935934", 1: "3543435095340509" etc... cars should array expect see: cars: array[4] allthough in db everythingg looks should:
"cars":["5607b0747eb3eefc225aed61","5607b07a7eb3eefc225aed62","5606bf4b0e76916c1d1668b4","5607b07a7eb3eefc225aed62"]}] last update: here next eachother react tools: (i have list of drivers , shows driver-object changes when gets selected)
state: choosendriver: {..} _v:0 _id: "235345353453" name: "roger" cars: ->_proto_ :{..} 0: "4242594395935934", _id: undefined same driver not choosen: drivers: array[3] ->0: {} ->1: {} _v: 0 _id: "4242594395935934" cars: array[1] so happens array when gets selected. weird...
if mean cars not array expect (and not this.context.foo.cars)...
the array constructor builds array each argument constructor element in returned array.
so, new array([1,2,3]) gives you: [[1,2,3]].
you want array.from([1,2,3]) give [1,2,3] appears want.
https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array
but code, why can't skip creating intermediate array , call this.context.foo.cars.map(...) directly?
Comments
Post a Comment