javascript - Mongoose FindOne not working -
i'm getting null response mongoose findone if put parameter datestring argument. i'm using nodejs , mongoose.
here code:
var service = app.models.service; controller.newservice = function(req, res) { var date = new date(); var datestring = date.tostring(); var counthours = 1; var user = req.decoded; var findprofessional = function(){ console.log(counthours); console.log(user._doc._id); console.log(req.body.service[0].tipo); console.log(datestring); service.findone({ tipo: req.body.service[0].tipo, client: user._doc._id, dateopen: datestring }, function(err, service) { console.log(service); if (err) throw err; if (!service) { console.log("service not found"); } else if (service) { if (service.status == 'open' && counthours <= 24) { settimeout(function(){ professional.find({ 'services.name': req.body.service[0].tipo }, function(err, professional) { if (err) throw err; if (professional) { stuff } else { stuff res.json({ success: false, message: 'no professionals found' }); }; }); counthours++; findprofessional(); }, 10000); } else if (service.status != 'open'){ // stuff } else if (counthours > 24){ //do stuff } } }); }; here schema:
var schemaservices = mongoose.schema({ tipo: { type: string, required: true }, client: { type: string, required: true }, dateopen: { type: string, required: true }, dateclose: { type: string, }, professional: { type: string }, status: { type: string, required: true }, services: { type: [{name: string, preco: string, tipo: string, tipocusto: string, pedido: string}], required: true }, visitday: { type: string }, visithour: { type: string }, address: { type: [{cep: string, street: string, number: string, comp: string, district: string, city: string}], required: true } }); schemaservices.index({client: 1, tipo: 1, dateopen: 1}, {unique: true}); what code do?
when request made route, node saves new service , looks professional job. , keep looking until timer counthours reachs 24 or haves professional work. server saving service without problems, need check status of service saved, , i'm facing problem of service.findone() returning null values when put dateopen: datestring.
the problem is
service.findone({ tipo: req.body.service[0].tipo, client: user._doc._id, dateopen: datestring } is returning null value service doesn't exists. if made same query in prompt using mongo, got right service.
i made tests , observed things:
- console.logs printing alright should print.
-when remove dateopen: datestring arguments in service.findone() works
so, believe error related datestring, can't see is.
hope can me,
thank in advance!
just solved own problem.
the problem execution time of node faster .save method of mongo, changed call of findprofessional() success callback of .save method. that:
newservice.save(function(err){ if (err) throw err; console.log('service saved successfully'); newservice.save(findprofessional()) }); now, working properly.
Comments
Post a Comment