I need to check if a field is unique (within the previous posts there was not the same name so within the database there is currently no other name that is the same with this one) using express validator or other technology (new to this so I do not know the best approach) in Node.js within the endpoints.
My current code is:
MainRouter.post('/animals', [
check('name')
.isAlpha()
.notEmpty()
.withMessage("The name should contain only letters and should be unique."),
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).send({errors: errors.array()});
}
AnimalPost.postAnimal(req.body).then(message => {
return res.json(message);
}).catch((error) => {
return res.json(404, error);
});
});
E.g.: When I POST a new name, for example "ChloeGiraffe", it should be unique, as it is currently not present within the database, which might look like this:
- DolphinDorly
- ElephantElle
- MonkeyMitter
- TurtleNinja
So, the ChloeGiraffe is validated and it is ready to be posted. Otherwise, if it would have already existed, then an error is displayed.
I do not know if it matters, as the work is done within the endpoints, but in case it matters I keep all the information in an Excel worksheet; I do not use MangoDB.
The POST itself works, it is just about the validation/checking of uniqueness.
How can I make this work? Please let me know if there is another desired approach to do this is mine if wrong, I could not find any useful information with regards to this one. Thank you very much in advanced!
I need to check if a field is unique (within the previous posts there was not the same name so within the database there is currently no other name that is the same with this one) using express validator or other technology (new to this so I do not know the best approach) in Node.js within the endpoints.
My current code is:
MainRouter.post('/animals', [
check('name')
.isAlpha()
.notEmpty()
.withMessage("The name should contain only letters and should be unique."),
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).send({errors: errors.array()});
}
AnimalPost.postAnimal(req.body).then(message => {
return res.json(message);
}).catch((error) => {
return res.json(404, error);
});
});
E.g.: When I POST a new name, for example "ChloeGiraffe", it should be unique, as it is currently not present within the database, which might look like this:
- DolphinDorly
- ElephantElle
- MonkeyMitter
- TurtleNinja
So, the ChloeGiraffe is validated and it is ready to be posted. Otherwise, if it would have already existed, then an error is displayed.
I do not know if it matters, as the work is done within the endpoints, but in case it matters I keep all the information in an Excel worksheet; I do not use MangoDB.
The POST itself works, it is just about the validation/checking of uniqueness.
How can I make this work? Please let me know if there is another desired approach to do this is mine if wrong, I could not find any useful information with regards to this one. Thank you very much in advanced!
Share Improve this question asked Jan 13, 2021 at 19:52 OliverOliver 5922 gold badges13 silver badges29 bronze badges 2- You say you are new so please don't take offense if this is something you already considered, but this is something which should be done using the "unique" constraint at the database level. You'd set up the database to only allow one instance of the name and when you try to write the 2nd instance to the database, the write/update will fail and you'd worry about that exception in Express (instead of trying to avoid the write/update which would cause the exception). – oooyaya Commented Jan 13, 2021 at 20:00
- I understand! Thank you very much, I consider your advice, I will work on the database level then! @oooyaya – Oliver Commented Jan 13, 2021 at 20:02
2 Answers
Reset to default 5use custom() validation rule
check('name')
.isAlpha()
.notEmpty()
.withMessage("The name should contain only letters and should be unique.")
.custom(value => {
return AnimalPost.findOne({ where: {name: value} })
.then(() => {
return Promise.reject('Name already taken')
})
}),
The find query can be changed as per your ORM, the above query is written by keeping in mind that you've used sequeulize ORM
To create a unique index, use the db.collection.createIndex() method with the unique option set to true.
db.members.createIndex( { "user_id": 1 }, { unique: true } )
MongoDB documentation:
https://docs.mongodb./manual/core/index-unique/
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744834094a4596158.html
评论列表(0条)