4

I am working on a project using node.js mongodb. My schema somewhat looks like:

var Doctor = new Schema({
    email : String,
    password : String,
    Dname : String,
    blockAppoint:[{
        day:String,
        sslot:[Number],
        eslot:[Number],
        address:String,
        status1:String
    }]
});

If I take all these values as input from user, I can't figure out how to insert into the array of nested objects. If my post api looks like:

var doc = new Doctor({
                email : req.body.email,
                password : req.body.password,
                name : req.body.Dname,
                blockAppoint:{
                              status1:req.body.xx,
                              day:req.body.day,
                              sslot:req.body.sslot,
                              eslot:req.body.eslot,
                              address:req.body.address
                            }
                });
doc.save(function(err){
                if(err){
                    res.send(err);
                    return;
                }
                res.json({
                    success: true,
                    message: 'doctor has been added!'   
                });
            });     

I'm able to input just one entry into the database. Does anyone know how do I change my api code so as to be able to take read input into my database.

1 Answer 1

3

Try adding the values to an array first using the push() method:

var sslot = [], eslot = [], blockAppoint = [];
sslot.push(req.body.sslot);
eslot.push(req.body.eslot);
blockAppoint.push({
    status1: req.body.xx,
    day: req.body.day,
    sslot: sslot,
    eslot: eslot,
    address: req.body.address
});

var doc = new Doctor({
    email: req.body.email,
    password: req.body.password,
    name: req.body.Dname,
    blockAppoint: blockAppoint
});
Sign up to request clarification or add additional context in comments.

3 Comments

and what about the blockAppoint array?How do i take multiple inputs for the blockAppoint object?
@default Apologies, I hadn't seen that as an array. Let me edit the answer to correct method
I'm using postman to test my code and on giving an input through urlencoded form this still gives an error on giving multiple entries.

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.