Assuming you’re using Mongoose and Express, in your controller you can do:
function findOneInput(req, res) {
Model.findOne({ field: req.query.input }, function (error, input) {
// Trata erro
if(error) {
console.error(error)
res.status(500).msg({ msg: 'Um erro ocorreu' })
}
// Manda o documento para o usuario
res.json(input)
})
}
Already in the view, you would need to make a request to your API, passing the input to the query. URI would be: example.com/api/nome-da-rota?input=valor-do-usuario
. The API would return you null
if the document does not exist in DB and returns the document if it exists. Then just do an if.
P.S.: That’s pretty general, it’s an assumption according to what you published. It may be that other adjustments have to be made regarding security, performance and even how you set your data.
Friend I think I will need a more detailed explanation what I want is to compare what the user insert in an input and I want to check if it exists in the database Do you understand? I think I explained myself as soon as I’ll rephrase in the post
– AlmostDone
That’s exactly what the code is doing. You receive this information from the user in an HTML file, and make a request in your API to check if there is a field with this data
– João Pedro Henrique