Javascript - Compare input field with mongodb field

0

Can anyone give me information about this? I'm trying to learn or else demonstration with explanation

What I want to do is to have input in html and go get the value of this input to check if it exists in a particular collection and if it exists it does something if it does not do something!

Thanks to those who help!

I'm a beginner in the field

    
asked by anonymous 29.06.2018 / 16:49

1 answer

1

Assuming you are 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)     
   })
}

In the view, you would need to make a request to your API, passing the input to the query. The 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 the DB and would return the document if it exists. Then just do an if.

Q.: That's pretty general, that's a guess according to what you posted. It may be that other adjustments have to be made in relation to security, performance and even the way you have adjusted your data.

    
29.06.2018 / 18:24