0

I have my route below that takes the form data and tries to input the page and archive into an existing name that is already in the mongodb. My form data is being sent correctly as I can see from my console.log.

Now I need to insert the page name and archive name where it equals name. Whether it already has data or not. They are already setup with array in the schema. You can see my schema below

Any ideas on how to get this setup?

router.post('/add-page', function(req, res, next){

    if(req.body.name && req.body.page && req.body.archive){
        //create object with form input
          var pageData = {
            client: req.body.name,
            page: req.body.page,
            archive: req.body.archive
          };

          console.log(pageData);
            //insert data
            Page.then(function(db){
                delete req.body._id;

                db.collection('pages').insertOne();
            });
            return res.render('index', { title: 'Home' });


    }else{

        var err = new Error('All fields required.');
        err.status = 400;
        return next(err);
    }
});


var ClientSchema = new mongoose.Schema({
  client: {
    type: String,
    required: true,
    trim: true,
    unique: true,
  },
  page: {
    type: [],
    trim: true,
  },
  archive: {
    type: [],
    trim: true,
  }
});
stolen
  • 1
  • I believe you should use something like findOneOrCreate, this may help: https://stackoverflow.com/a/47598064/942748 – Teh SoTo Oct 08 '18 at 21:26
  • It seems to me that you're looking for `findOneAndUpdate()` ... Check out the official [docs](https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate) – Dženis H. Oct 09 '18 at 00:17

0 Answers0