如何在创建时为MongooseJS数组添加值

时间:2016-08-08 14:30:21

标签: javascript mongoose

我的city架构看起来像这样:

...
name: String,
reviews: [{
  user: String,
  text: String
}],
...

我使用

创建一个新的city
var city = new City({
  name: "London",
  reviews: {user: "test user", text: "This is the review"}
});

city.save(function(err){
  //check for err
  //do stuff
});

但我收到此错误:

CastError:施放到数组失败的值" [object Object]"

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您正在评论中传递一个对象来代替数组。将其作为

传递
var city = new City({
  name: "London",
  reviews: [{user: "test user", text: "This is the review"}]
});