Saturday, 19 October 2019

Query MongoDB problems

I'm trying to make an API, so in my router I have callback function, that use mongoose schema.using Model.find().limit(parameters).skip(otherparameters).exec((err,res)=>{Model.countDocuments().exec((err,count)=>{ console.log(res)})})Sometimes, it's print the data from DB, and sometimes not, without change anything, without any network problems.I'm confuse. (Testing with postman, and testing with browser network tools)Here is the original code: (LogsModel is the schema)logs_router.post('/loadata',(req,res)=>{ console.log("length is:"+req.body['length']+" start is:"+req.body['start']) try{ LogsModel.find({}).limit(Number(req.body['length'])).skip(Number(req.body['start'])).exec((err,logs)=>{ LogsModel.countDocuments().exec((err,count)=>{ console.log(logs) const resJson = {} resJson['draw'] = 2 resJson['data'] = logs resJson['recordsTotal'] = count resJson['recordsFiltered'] = count resJson['error'] = err res.jsonp(resJson) }) }) } catch (err){ console.log(err) } }) My next problem, is when I remove "console.log(logs)" printing, nothing is coming back :O

Submitted October 19, 2019 at 04:29PM by XDavidT

No comments:

Post a Comment