Jerevick
Jerevick

Reputation: 61

Mongoose Update

I can't update the status field in the document. can somebody please help? Thanks! changing the default value is the issue.

i want the admin to be able to update the status of the applicant from pending to whatever option is chosen from the select options in the ejs form

this is in the controller:

 exports.postName = async(req, res, next)=>{
     const name = req.body.name;
    console.log(name)
     const pupil = new Pupil({
         name: name,
     });

    await pupil.save().then(updates=>{
         console.log(updates)
         res.redirect('/name');
     }).catch(error=>{
         console.log(error);
     });
 }

this also is from the controller:

 exports.postUpdatePupil = async (req, res, next)=>{        
    const status = req.body.status
             Pupil.updateOne(
                 {_id: req.params.id},
                {
                    $set: {'Pupil.$.status': status}
           
                },(err, result)=>{
                    if(err){
                        return next(err)
                    }console.log(result);
                }
            )

this is the model:

const UpdateSchema = new mongoose.Schema({
    name: String,
    status: {type: String, default: "Pending"}
});

const update = mongoose.model('update', UpdateSchema);

module.exports = update;

Upvotes: 0

Views: 241

Answers (1)

Jumblar
Jumblar

Reputation: 11

Model name should be "Pupil" and not "Update".

const PupilSchema = new mongoose.Schema({
    name: String,
    status: {type: String, default: "Pending"}
});

module.exports = mongoose.model('Pupil', PupilSchema);

And you update like:

Pupil.updateOne(
   { _id: req.params.id },
   status // same as { status: status }
   (err, result) => {
      if (err) return next(err);
      console.log(result);
   },
);

Upvotes: 1

Related Questions