我正在尝试将数组添加到Mongo文档中,并且出现错误"Cast to Array failed for value "[object Object],[object Object]" at path "vendors""

这是我的模型:

module.exports = {
  attributes: {
    vendors: {
      type: [String]
    },
    description: {
      type: String
    }
  }
};


这是我要创建的代码:

var vendors = ko.observableArray(['foo','bar']);
var desc = ko.observable('yadda yadda yadda');
var dto = {
    data: {
      vendors: vendors(),
      description: description()
    }
};
DataService.quoteRequest.create(dto);

最佳答案

音乐海岸

我不熟悉ockoutout.js,但是就Mongoose而言,您似乎并没有定义SchemaModel。您需要执行以下操作:

定义架构

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var attributesSchema = new Schema({
  vendors: [String],
  description: String
});


建立模型

var Attributes = mongoose.model('Attribute',attributesSchema);


创建和保存文档

//create an attribute document
var attribute = new Attributes(
   {
     vendors: vendorsArray,
     description: desc
    }
 );

attribute.save(function(err){
if(!err) console.log('Success');
}

07-28 09:15