使用等待时节点响应中的复制问题

时间:2018-12-20 06:08:00

标签: angularjs node.js mongodb

我正在发送angularjs请求并获得响应,有时会出现重复

 $http.post('/project_inspection_list',{project_id:$scope.projectid}).then(function(response){
       if(response){
           if(response.data.status =="success"){
             setTimeout(() => {

                $scope.inspectionlist = response.data.inspection_list
                console.log($scope.inspectionlist)
                $scope.page_loader =  false;
             }, 1);
           }
       }
   })

正确的响应:

[{
inspection_id: "000000009664ea3b2c3131c2", 
inspection_name: "Sample", 
field_user_name: "Radisson Blue Marina", 
published_date_org: "2018-12-18T09:23:59.403Z", 
published_date: "2018-12-18T09:23:59.403Z"
}
{
inspection_id: "000000009664ea3b2c3131cb", 
inspection_name: "Sample 2", 
field_user_name: "Radisson Blue Marina", 
published_date_org: "2018-12-18T09:25:10.156Z", 
published_date: "2018-12-18T09:25:10.156Z"
}
{
inspection_id: "000000009664ea3b2c3131d0", 
inspection_name: "Sample 3", 
field_user_name: "Radisson Blue Marina", 
published_date_org: "2018-12-18T09:30:02.730Z", 
published_date: "2018-12-18T09:30:02.730Z"
}]

有时候会收到如下响应:

[{
inspection_id: "000000009664ea3b2c3131c2", 
inspection_name: "Sample",
field_user_name: "Radisson Blue Marina", 
published_date_org: "2018-12-18T09:23:59.403Z", 
published_date: "2018-12-18T09:23:59.403Z"
}
{
inspection_id: "000000009664ea3b2c3131d0", 
inspection_name: "Sample 3", 
field_user_name: "Radisson Blue Marina", 
published_date_org: "2018-12-18T09:30:02.730Z", 
published_date: "2018-12-18T09:30:02.730Z"
}
{
inspection_id: "000000009664ea3b2c3131d0", 
inspection_name: "Sample 3", 
field_user_name: "Radisson Blue Marina", 
published_date_org: "2018-12-18T09:30:02.730Z", 
published_date: "2018-12-18T09:30:02.730Z"
}]

节点代码:

app.post('/project_inspection_list', async function (req, res) {

    var id = req.body.project_id;

    try{
    var response = await mongo.inspection.findOne({'projectID':id})
    }
    catch(err){
     return res.json({status:"failure"})

    }
    var inspection = [];

    if(response){
    var data = response.inspection_data;

    for ( var i = 0; i<data.length; i++){
        var item = data[i]
        var fielduser = item.userID
        // var user = 
        // if(item.published == "Y"){

            try{

                var client = await mongo.user.findOne({'_id': mongoose.Types.ObjectId(response.clientID)});
                }
                catch(err){
                    res.json({status:"failure"})

                }
        if(client){
        await mongo.fielduser.findOne({'_id': mongoose.Types.ObjectId(fielduser)},function(err,user){
            if(err){
                res.json({status:"failure"})
            }
            else{
                var domain_path = url_path.format({ protocol: req.protocol,host: req.get('host')});   
                var path ;
                if(item.pdf){
                 path = domain_path+"/uploads/docs/"+client.owner_name+'/inspection/pdf/'+item.pdf;
                }
                inspection.push({
                    inspection_id:item._id,
                    inspection_name:item.insName,
                    field_user_name : user.owner_name, 
                    published_date_org:item.published_date_org,
                    published_date:item.published_date,

                    path : path,
                    published : item.published,
                    created_date : item.created_date,
                    submissionTime : item.submissionTime

                });
            // });
            }

        })
    }
    else{

        res.json({status:"failure"})
    }

    // }
   }
   console.log(inspection)
   res.json({status:"success","inspection_list":inspection})
}
else{
    res.json({status:"success","inspection_list":inspection})
}
});

实际上是有inspection_name样本,样本2和样本3,有时会收到错误的响应样本,样本3和样本3-两次获得样本3而不是样本2。即使在某个时间

1 个答案:

答案 0 :(得分:0)

我已通过使用Lookup解决了该问题

app.post('/project_inspection_list', async function (req, res) {
    var id = req.body.project_id;
    mongo.inspection.findOne({'projectID':id},function(err,clientdata){
   if(clientdata){
       console.log(clientdata.clientID)
       console.log(id)
    mongo.user.findOne({'_id': mongoose.Types.ObjectId(clientdata.clientID)},function(err,client){
        if(client){
            mongo.inspection.aggregate([
                { $unwind: "$inspection_data" },

                { $lookup:
                    {
                    from: 'fieldusers',
                    localField: 'inspection_data.userID',
                    foreignField: '_id',
                    as: 'inspection_list'
                    }
                },


        { $unwind: "$inspection_list" },

        { $match: { 'projectID':mongoose.Types.ObjectId(id)}},


        {"$project": {
         "_id" : 0,
         "name" : "$inspection_list.owner_name",
         "inspection_id": "$inspection_data._id",
         "inspection_name": "$inspection_data.insName",
         "field_user_name" : "$inspection_list.owner_name",
         "published_date_org": "$inspection_data.published_date_org",
         "published_date": "$inspection_data.published_date",
         "published" :  "$inspection_data.published",
         "created_date" : "$inspection_data.created_date",
         "submissionTime" : "$inspection_data.submissionTime"
        }}

        ]) .exec(function(err, result){
            if(err){
                res.json({status:"success","inspection_list":[]})

            }
            else{
                res.json({status:"success","inspection_list":result})
            }

        })
        }
        else{
            res.json({status:"success","inspection_list":[]})
        }
    })
}
else{
    res.json({status:"success","inspection_list":[]})
}
})

});