I want to let the front end know if there is a validation issue such as a duplicate submission ID. I have ready many stack posts already and tried the various solutions in this code…
in short none of the catch methods are catching an error. It as though mongo or mongoose is throwing it before my code gets a look in 🙁
here is my code
exports.createFinishedJob = async (req, res) => {
try {
console.log('createFinishedJob req.body', req.body);
const finishedjob = new FinishedJob();
if (req.body.jobSubmissionId) { finishedjob.jobSubmissionId = req.body.jobSubmissionId };
if (req.body.customerId) { finishedjob.customerId = new ObjectId(req.body.customerId) };
finishedjob.finishedjob = req.body.finishedjob;
if (req.body.other) { finishedjob.other = req.body.other };
console.log('createFinishedJob', finishedjob);
let error;
try {
await finishedjob.save()
.catch(err => console.log("HALLO err", err));
// if (res && doc) {
// return res.json(doc);
// } else {
// return doc
// }
} catch (err) {
error = err;
console.log("createFinishedJob error", error)
}
} catch (err) {
console.error("Something went wrong")
console.error(err)
}
};
here is the error:-
(node:85241) UnhandledPromiseRejectionWarning: MongoServerError: E11000 duplicate key error collection: Genaich.processingjobs index: jobSubmissionId_1 dup key: { jobSubmissionId: "1006006" }
at /Users/urfx/phos-code/genaich/node_modules/mongodb/lib/operations/insert.js:50:33
at /Users/urfx/phos-code/genaich/node_modules/mongodb/lib/operations/command.js:84:64
at processTicksAndRejections (internal/process/task_queues.js:95:5)
(node:85241) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
here is the model
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const {ObjectId} = mongoose.Schema;
const SchemaTypes = mongoose.Schema.Types;
const finishedjobSchema = new Schema(
{
customerId: {
type: ObjectId,
ref: "User",
required: false
},
jobSubmissionId:{
type: String, // if we don't get one from the customer use payment / order number
unique: true,
index: true
},
trashed: {
type: Boolean
},
finishedjob: {
type: Object
},
other: {
type: SchemaTypes.Mixed // to start the ball rolling
}
},
{ timestamps: true });
let FinishedJob = mongoose.model("FinishedJob", finishedjobSchema);
module.exports = FinishedJob;
2
Answers
Many thanks @jQueeny
Your first suggestion using "create" does what I need with a slight tweak I should have mentioned I also wanted to return the successful new doc too. I couldn't put all this in the comments.
Option one would be to use the
Model.create()
method and just catch any errors that it throws. TheModel.create()
is just like aconst doc = new Model();
+doc.save()
all in one method. Use like so:Option two would be to quickly check if a
Finishedjob
withjobSubmissionId
already exists and return the error message early. However, if there is no match then you still need to create the new document again which means two calls to the database so if you are handling thousands of new requests to that route per minute then obviously less efficient. Use like so: