1

I have a collection model like this:

const Task = mongoose.model('Task', {
  description: {
    type: String,
    trim: true,
    required: true
  },
  completed: {
    type: Boolean,
    default: false
  }
})

Currently, When a user tries to update a document using the API, any field that is not in the schema will be ignored, and the document will be updated. I want to throw an error if the user sends an update request to the API with a field that is not available in the database.

if I have a task with id of 12345 in the database:

{
  _id: 12345,
  description: "Buy cheese."
  completed: false
}

and a user sends an update query to the API for a the task:

id = '12345'
updates = {
  description: 'Buy Milk',
  due: '1 Week' //<-- Invalid Field
}

And I use this object to update the document:

await Task.findByIdAndUpdate(id, updates)

mongoose completely ignores the invalid due field and updates the document with the new description field.

Is there a clean way to avoid this sort of invalid update requests?

  • Possible duplicate of [How to check if that data already exist in the database during update (Mongoose And Express)](https://stackoverflow.com/questions/16882938/how-to-check-if-that-data-already-exist-in-the-database-during-update-mongoose) – Sai Aug 05 '19 at 09:15
  • @SaiKrishna that is a different question. I am trying to update an existing document with the data provided from the client through an API endpoint. I want to make sure that the client receives an error if he sends invalid fields for update, instead of just ignoring the unknown fields. – Ali Mousavi Aug 05 '19 at 12:02

2 Answers2

1

There is a way i found which is:

const params = Object.keys(req.body) // req.body is the updates coming from API
const allowedMethods = ["completed", "description"];
const isValidOperation = params.every((param) => allowedMethods.includes(param));

if (!isValidOperation) {
   return res.status(400).send({ error: "invalid update" });
}
AndrewL64
  • 15,794
  • 8
  • 47
  • 79
Zaid abu khalaf
  • 776
  • 8
  • 15
0
While updating add in criteria the key that comes from the front end with $exists if it's not found in database update will not return any data and you can throw an error in that case.

criteria will be {_id:id};
update will be {description: 'Buy Milk'};
if(payload.due){
   criteria.due:{$exists:true},
   update.due=payload.due
}

let updatedData= await Task.update(criteria,update) if(updatedData.n== 0) throw error invalid parameters passed

// https://docs.mongodb.com/manual/reference/method/db.collection.update/ and this only use one query

sushant mehta
  • 1,244
  • 1
  • 7
  • 13
  • I don't think this answer solves my problem. Or maybe I don't understand what you are suggesting. – Ali Mousavi Aug 05 '19 at 12:07
  • You need to check if invalid key even exists in query while update if it exists it'll update the document else it won't and if no match occurs while update query you would know that the key sent is invalid and can send error in response body – sushant mehta Aug 05 '19 at 12:16