Sunday, 16 April 2017

How Can I Use .find() To Find Documents in DB For Logged In User?

On the backend I have this for my 'get' request to my '/logs' collection. The data comes back as an array called "times":

router.get('/', (req, res) => {
time
.find({'userName':  req.params.userName}) 
.exec()
.then(times => {
  console.log(req.params.userName)
  //console.log(times)
  res.json({
    times: times.map(
      (time) => time)
  });
})
.catch(
  err => {
    console.error(err);
    res.status(500).json({message: 'Internal server error'});
});

});

I am trying to display times for which ever user is logged in and I can do that however the network tab still shows all times for all users instead of the user who is logged in. I have this on the front end making the request:

function getTimesFromDB(callback) {
var user = state.loggedIn;
$.ajax({
    type: 'GET',
    dataType: 'json',
    url: '/logs',
    'headers': {
        "content-type": "application/json",
    },
    'data': {
        'userName': user,
    },
    success: callback,
})

}

Any help would appreciated. thanks!



via JontheNerd

No comments:

Post a Comment