0

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:[]

})
1
  • 1
    there's no model argument while saving the post. Commented Dec 3, 2017 at 5:47

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.