Mongoose无法保存模型数组

时间:2016-03-14 19:01:43

标签: node.js mongodb mongoose

我试图这样做:

var hStatus = require('models / hStatus');

var log = new hLog();

    hStatus.find({_id: /^value_/}, function(err, data){
            if(!err){
                    log.h = data;
                    log.save(function(err){
                         if(err){
                            console.log(err);
                         }else{
                            console.log('ok!');
                         }
                    });
            }
    });

数据var结构:

[ { outs: 
     [ [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object] ],
    c: 1457739456474,
    b: false,
    c: true,
    d: false },

我的模特是:

var hLog = new mongoose.Schema({
        a: Date,
        b: Number,
        c: String,
        d: String,
        e: Boolean,
        f: Boolean,
        g: String,
        h: [hStatus]
});

//this two same file
var hOuts = new mongoose.Schema({
        a: Number,
        b: String,
        c: Boolean,
        d: Boolean
});

var hStatus = new mongoose.Schema({
        a: String,
        b: Boolean,
        Outs: [hOuts],
        c: Number,
        d: Boolean   
});

但是保存功能返回:

ValidationError:CastError:Cast to Array因“{Outs:

的值而失败

这是正确的方法吗?

我不确定我错过了什么。

3 个答案:

答案 0 :(得分:0)

尝试 .push()方法:

var log = new hLog();

    hStatus.findOne({_id: /^value_/}, function(err, data){
            if(!err){
                    log.h.push(data);
                    log.save(function(err){
                         if(err){
                            console.log(err);
                         }else{
                            console.log('ok!');
                         }
                    });
            }
    });

答案 1 :(得分:0)

尝试以下

 let alertAction = UIAlertAction(title: "Ok", style: .Default) { (action) -> Void in
        self.navigationController?.pushViewController(vc as HomeViewController, animated: true)

    }

答案 2 :(得分:0)

您在hStatus架构中有错误

Hash: 78d489a6e4aec65292b2  
Version: webpack 1.12.14
Time: 8161ms
                         Asset       Size  Chunks             Chunk Names
vendor-78d489a6e4aec65292b2.js     925 kB       0  [emitted]  vendor
index-78d489a6e4aec65292b2.js    1.87 kB       1  [emitted]  index
app-78d489a6e4aec65292b2.js    1.41 kB       2  [emitted]  app
index-78d489a6e4aec65292b2.css   83 bytes       1  [emitted]  index
vendor-78d489a6e4aec65292b2.js.map    1.05 MB       0  [emitted]  vendor
index-78d489a6e4aec65292b2.js.map     2.3 kB       1  [emitted]  index
index-78d489a6e4aec65292b2.css.map  107 bytes       1  [emitted]  index
app-78d489a6e4aec65292b2.js.map    1.76 kB       2  [emitted]  app
index.html  370 bytes          [emitted]  
app.html  311 bytes          [emitted]  
[0] multi vendor 64 bytes {0} [built]
+ 258 hidden modules
Child html-webpack-plugin for "index.html":
+ 3 hidden modules
Child html-webpack-plugin for "app.html":
+ 3 hidden modules
Child extract-text-webpack-plugin:
+ 2 hidden modules
在你的json数据中

c: Number,
d: Boolean   

类型不匹配