2016-04-25 80 views
0

我需要找到PERSON1 deals..so的歷史在我getHistory模塊我已篩選所有的交易在PERSON1的 dealSchema,在acceptreject功能我用聚合概念在已接受的字段中篩選具有person1的 _d的交易。但回調函數acceptreject不取它總是會導致[]的結果..請人findout的錯誤,我已經在這個code..Thanks提前節點JS聚集和findone概念

var acceptreject = function (userId, callback){//callback function for 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 but person1 accepted the deal 
     mydeal: dealObj 
    } 
    next(res.send(200, final)); 
    } 
    }); 
    }); 
    } 

做,我的模式是

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); 

回答

0

我在回調函數中犯了一個錯誤,其中該函數的userId參數沒有通過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); 
}) 
}