简体   繁体   中英

Mongoose saving empty array

I have a array and i trying to insert it with mongoose but in return i got a empty array what i'm doing wrong,

my schema:

let postSchema = mongoose.Schema({


    date            : { type: Date, default: Date.now },
    name            : String,
    desc            : String,
    type            : []


},{collection: 'Post'});

my insert:

console.log(req.body.type); //here i have something like ["Teste1","Teste2"]
let post = new Post({ 
                       name: req.body.descricao, 
                       desc: req.body.desc
                       type: req.body.type
                    });

post.save((err, model) => {

    if (err) console.log(err);

    if (model) console.log(model); //ALL INSERTED but array is just type:[]

})

The best way is to specify the type of the array elements. For example,

type: [String]

Specifying an empty array is equivalent to Mixed type. Also check the type of req.body.type

console.log(typeof req.body.type)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM