猫鼬,数组中的对象 [英] Mongoose, array in object

查看:135
本文介绍了猫鼬,数组中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:
这个问题似乎猫鼬是相关的。请参阅更新底部。

在我用下面的code通过父 choosenItem 来一个孙子:

  childContextTypes:{
        富:React.PropTypes.object.isRequired,
         },   getChildContext:功能(){
        返回{富:this.state.choosenDriver}
   },

choosenDriver是看起来像这样一个猫鼬模型:

  VAR模式= mongoose.Schema;
VAR driverSchema =新mongoose.Schema({  司机:字符串,
  年龄:字符串,
  汽车:[{类型:Schema.Types.ObjectId,REF:汽车'}]});
module.exports = mongoose.model('司机',driverSchema);

这工作正常,并在孙我能做到以下几点:

  contextTypes:{
      富:React.PropTypes.object.isRequired,
  },//一些其他的东西...渲染:功能(){
  返回(
            < H1> {this.context.foo.name}< / H1>
          )
});

这显示的名称 choosenDriver

我也想通过汽车循环,以为可以这样进行:

 渲染:功能(){   VAR汽车=新阵列(this.context.foo.cars);
   VAR driversCars = cars.map(功能(车,我){
   返回<李键= {I}> {}汽车与LT; /李取代;
   });
  返回(
         < D​​IV>
            < H1> {this.context.foo.name}< / H1>
            < UL> {driversCars}< / UL>
         < / DIV>  )
} });

这将返回所有的司机车在一个单行显示的字符串。
我参加了反应开发商工具一看,只见那车没有被定义为一个数组。

这是可能是因为 choosenDriver 状态和数组不以这种方式支持?

赛车存在明显但不是作为一个阵列。

如何进入汽车为Array在这个例子中的任何想法?

感谢您!

更新:

在寻找在反应的工具我看到这个this.context.foo.cars:

 汽车:
- > _proto_:{..}
0:4242594395935934
1:3543435095340509
等等...汽车应该是一个数组,所以我希望看到:
汽车:数组[4]

Allthough在DB everythingg看起来它应该:

<$p$p><$c$c>\"cars\":[\"5607b0747eb3eefc225aed61\",\"5607b07a7eb3eefc225aed62\",\"5606bf4b0e76916c1d1668b4\",\"5607b07a7eb3eefc225aed62\"]}]

最后更新:
在这里,他们是旁边的海誓山盟我反应过来工具:
(我有驱动程序的列表,这表明,当它被选中的驱动对象的变化)

 状态:
choosenDriver:{..}
 _V:0
 _id:235345353453
 名称:罗杰
 汽车:
     - &GT; _proto_:{..}
    0:4242594395935934
   _id:未定义
相同的驱动程序但不choosen:
司机:数组[3]
  - 大于0:{}
  - →1:{}
      _V:0
      _id:4242594395935934
      汽车:数组[1]

因此​​,当它被选中有事到阵列。奇怪...


解决方案

如果你的意思是汽车是不是您所期望(而不是此阵.context.foo.cars )...

使用Array构造函数建立与每个构造函数的参数作为返回数组中的元素的数组。

因此​​,新的Array([1,2,3])为您提供: [1,2,3]

您想 Array.from([1,2,3]),这将给你 [1,2,3] 这似乎是你想要的。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

但你与code,为什么你就不能跳过创建中间阵列并调用 this.context.foo.cars.map(...)

Update: This problem seems to be related to mongoose. Please see update at the bottom.

In the parent I use the following code to pass choosenItem to a grandChild:

childContextTypes: {
        foo: React.PropTypes.object.isRequired,
         },

   getChildContext: function() {
        return { foo: this.state.choosenDriver }
   },

choosenDriver is a mongoose Model that looks like 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 and in the GrandChild I can do the following:

contextTypes: {
      foo: React.PropTypes.object.isRequired,
  },

//some other stuff...

render: function(){
  return (
            <h1>{this.context.foo.name}</h1>
          )
});

And this displays the name of the choosenDriver.

I would also like to loop through the cars and thought it could be done like 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 all the drivers cars in a string displayed on one single line. I took a look in react developers tool and saw that cars is not defined as an Array.

Is this maybe because choosenDriver is a state and arrays are not supported in this manner?

The cars obviously exists but not as an Array.

Any ideas on how to access cars as an Array in this example?

Thank you!

Update:

When looking at this.context.foo.cars in react-Tools i see this:

cars: 
->_proto_ :{..} 
0: "4242594395935934", 
1: "3543435095340509"
etc...

cars should be an Array so I would expect to see:
cars: Array[4]

Allthough in the DB everythingg looks as it should:

"cars":["5607b0747eb3eefc225aed61","5607b07a7eb3eefc225aed62","5606bf4b0e76916c1d1668b4","5607b07a7eb3eefc225aed62"]}]

Last Update: Here they are next to eachother i react Tools: (I have a list of drivers and this shows that the Driver-object Changes when it gets selected)

State:
choosenDriver: {..}
 _v:0
 _id: "235345353453"
 name: "Roger"
 cars: 
    ->_proto_ :{..} 
    0: "4242594395935934", 
   _id: undefined 


Same driver but not choosen:
drivers: array[3]
 ->0: {}
 ->1: {}
      _v: 0
      _id: "4242594395935934"
      cars: Array[1]

So something happens to the Array when it gets selected. Weird...

解决方案

If you mean cars is not the Array you expect (and not this.context.foo.cars)...

The Array constructor builds an array with each argument to the constructor as an element in the returned array.

So, new Array([1,2,3]) gives you: [[1,2,3]].

You want Array.from([1,2,3]) which will give you [1,2,3] which appears to be what you want.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

But with your code, why can't you just skip creating the intermediate array and just call this.context.foo.cars.map(...) directly?

这篇关于猫鼬,数组中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆