Skip to content
Advertisement

Res value is null in an app.get call done from vue.js front-end to express back-end

I am calling this code from the front-end and confirmed that there is a proper db connection and that the Id value is properly passed, and that there is a corresponding value in the database, but for some reason, res is null. What am I missing?

app.get("/api/walletlogin/user/:userId", (req, res) => {

 id = req.params.userId
 var query = {_id: id}
  db.collection("Users").findOne(query, (err, result) => {
  if (result) {
    console.log(result.userName)
  } else {
    console.log('No User')
  }
})

Here is the front-end call:

  axios.get('/api/walletlogin/user/' + accounts)
      .then((response) => {
        console.log('Logged in With ' + accounts)
        router.push('/account')
      })
      .catch((errors) => {
        console.log('Cannot log in')
      })
  }).catch((err) => {
    console.log(err, 'err!!')
  })

Advertisement

Answer

You could try to convert your id to an objectID.

var ObjectId = require('mongodb').ObjectId; 
var id = ObjectId(req.params.userId);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement