Ankur Vishnoi
Ankur Vishnoi

Reputation: 81

Mongoose not saving document completely

I am trying to save data to my mongo db using mongoose. But unfortunately I am not able to save it completely. However it creates a data array but defaults like company name etc. are not saving. However these values are not available in requested body

enter image description here

I am using:

var seller = new sellers(req.body);

  seller.save(function (err) {
    if (err) {
        return res.status(500).send('An user with this email id or mobile number already exist');
    }
    res.status(200).send('You have successfully registered');
})

Upvotes: 0

Views: 109

Answers (1)

Steve Holgado
Steve Holgado

Reputation: 12071

In this case you could use a pre save hook to set an object as the default in your array:

userSchema.pre('save', function(next) {
    if (this.data.length == 0) {

        var default = {
            fieldName: 'Company Name',
            fieldValue: 'No Information Provided',
            // etc.
        };

        this.data.push(default);

    }
    next();
});

Upvotes: 1

Related Questions