javascript - Node js aggregation and findone concept -
i need find history of person1 deals..so in gethistory module have filtered deals in person1's dealschema , in acceptreject function have used aggregation concept filter deals have person1's _d in accepted field. callback function acceptreject not fetch result results in []..anyone please findout mistake have done in code..thanks in advance
var acceptreject = function (userid, callback){//callback function finding accepted deal console.log("aggregate:" +userid); if(userid == null && userid == 'undefined'){ console.log("error fetching id"); res.send(new response.respondwithdata('failed','invalid userid')); return next(); } user.aggregate([ { $unwind: "$deals" }, { "$match": { "deals.accepted": userid //"deals.rejected": id } }, { $project:{ "shopname":"$deals.shopname", "deal":"$deals.deal", "price":"$deals.price", "name":"$name" } } ],function (err, user){ console.log(user); if (err){ callback (err); } console.log("your accepted deal:"+ user); // res.send(200,user); callback(null, user); }) } exports.gethistory = function (req, res, next) { var incominguser = req.user; var id = incominguser._id; user.findbyid(id,function (err, user){ console.log(user); if (err) { return next(new response.respondwithdata('failed','cant find theuser')); } var dealobj = _.filter(user.deals); acceptreject({ userid : id },function(err, users){ if(err){ res.send(new restify.internaerror(err.message)); return next(); } else{ console.log("final"); var final = { accepteddeal: users,//it returns [] in result person1 accepted deal mydeal: dealobj } next(res.send(200, final)); } }); }); }
and schema is
var dealschema = new mongoose.schema({ shopname: {type: string,required: true}, deal: {type: string,required: true}, price:{type: number,required: true}, start:{type: date,default: date.now}, end:{type: date}, expiry:{type: date}, comments:{type: string}, accepted: {type:mongoose.schema.types.objectid, ref:'user'},//person1 _id rejected: {type:mongoose.schema.types.objectid, ref: 'user'} }); var userschema = new mongoose.schema({ name: { type: string,required: true}, phone: { type: number, required: true,unique: true}, email:{type: string}, password: {type: string}, deals:[dealschema] }, {collection: 'user'}); mongoose.model('deal', dealschema); mongoose.model('user', userschema);
i have done mistake in callback function userid parameter of function not pass id
var accept= function (userid, callback){ console.log("aggregate:" +userid); if(userid == null && userid == 'undefined'){ res.send(new response.respondwithdata('failed','invalid userid')); return next(); } var id = new object(userid); console.log(id); user.aggregate([ { $unwind: "$deals" }, { "$match": { "deals.accepted" : id } }, { $project:{ "shopname":"$deals.shopname", "deal":"$deals.deal", "price":"$deals.price", "name":"$name" } } ],function (err, user){ console.log(user); if (err){ callback(err); } console.log("your accepted deal:"+ user); //res.send(200,user); callback(null, user); }) }
Comments
Post a Comment