Javascript for循环变量没有返回值

时间:2018-05-18 12:06:29

标签: javascript mongodb loops for-loop

第一个代码是我希望控制台日志返回的结果:

for(var inspectionItem in parameters) {
    var Agent = parameters[inspectionItem];
    if(Agent.length == 24) {
        for(var inspectionItem in parameters) {
            var rating = parameters[inspectionItem];
            if(inspectionItem.length == 24) {
                console.log(rating);
                if(rating == 1 || rating == 2 || rating == 3 || rating == 4) {
                    inspectionTemplate.findOne({'user': req.user._id, '_id': inspectionItem}).exec(function(err, inspectionResult) {
                        if(err) throw err;
                        if(inspectionResult != null || inspectionResult != '') {
                            Inspection.findOne({'user': req.user._id, 'propertyID': id, 'name': inspectionResult.name, 'day': d.getDate(), 'month': d.getMonth, 'day': d.getDate()}, function(err, inspectionResults) {
                                if(err) throw err;
                                if(inspectionResults == '' || inspectionResults == null) {
                                    var newInspection = Inspection();
                                    newInspection.user = req.user._id;
                                    newInspection.propertyID = id;
                                    newInspection.name = inspectionItem;
                                    newInspection.order = inspectionResult.order;
                                    newInspection.agent = Agent;
                                    newInspection.rating = rating;

                                    newInspection.year = d.getFullYear();
                                    newInspection.month = d.getMonth();
                                    newInspection.day = d.getDate();
                                    /*newInspection.save(function(err) {
                                        if(err) throw err;
                                    });
                                    console.log(newInspection);*/
                                }
                            });
                        }   
                    });
                }
            }
            if(inspectionItem == 'remarks') {
                Inspection.findOne({'user': req.user._id, 'propertyID': id, 'day': d.getDate(), 'month': d.getMonth, 'day': d.getDate()}, function(err, inspectionResults) {
                    if(err) throw err;
                    if(inspectionResults != '' || inspectionResults != null) {
                        var newRemark = Inspection();
                        newRemark.user = req.user._id;
                        newRemark.propertyID = id;
                        newRemark.agent = Agent;
                        newRemark.name = 'Remarks';
                        newRemark.remarks = req.body[inspectionItem];
                        newRemark.year = d.getFullYear();
                        newRemark.month = d.getMonth();
                        newRemark.day = d.getDate();
                        /*newRemark.save(function(err) {
                            if(err) throw err;
                        });*/
                    }
                });
            }
        }
    }
    break;
};

但是这段代码的问题在于它不在我需要它的函数范围内。

第一个代码的结果是:

First code result.

我想要的范围内的第二个代码不起作用:

for(var inspectionItem in parameters) {
    var Agent = parameters[inspectionItem];
    if(Agent.length == 24) {
        for(var inspectionItem in parameters) {
            var rating = parameters[inspectionItem];
            if(inspectionItem.length == 24) {

                if(rating == 1 || rating == 2 || rating == 3 || rating == 4) {
                    inspectionTemplate.findOne({'user': req.user._id, '_id': inspectionItem}).exec(function(err, inspectionResult) {
                        if(err) throw err;
                        if(inspectionResult != null || inspectionResult != '') {
                            Inspection.findOne({'user': req.user._id, 'propertyID': id, 'name': inspectionResult.name, 'day': d.getDate(), 'month': d.getMonth, 'day': d.getDate()}, function(err, inspectionResults) {
                                if(err) throw err;
                                if(inspectionResults == '' || inspectionResults == null) {
                                    var newInspection = Inspection();
                                    newInspection.user = req.user._id;
                                    newInspection.propertyID = id;
                                    newInspection.name = inspectionItem;
                                    newInspection.order = inspectionResult.order;
                                    newInspection.agent = Agent;
                                    newInspection.rating = rating;
                                    console.log(rating);

                                    newInspection.year = d.getFullYear();
                                    newInspection.month = d.getMonth();
                                    newInspection.day = d.getDate();
                                    /*newInspection.save(function(err) {
                                        if(err) throw err;
                                    });
                                    console.log(newInspection);*/
                                }
                            });
                        }   
                    });
                }
            }
            if(inspectionItem == 'remarks') {
                Inspection.findOne({'user': req.user._id, 'propertyID': id, 'day': d.getDate(), 'month': d.getMonth, 'day': d.getDate()}, function(err, inspectionResults) {
                    if(err) throw err;
                    if(inspectionResults != '' || inspectionResults != null) {
                        var newRemark = Inspection();
                        newRemark.user = req.user._id;
                        newRemark.propertyID = id;
                        newRemark.agent = Agent;
                        newRemark.name = 'Remarks';
                        newRemark.remarks = req.body[inspectionItem];
                        newRemark.year = d.getFullYear();
                        newRemark.month = d.getMonth();
                        newRemark.day = d.getDate();
                        /*newRemark.save(function(err) {
                            if(err) throw err;
                        });*/
                    }
                });
            }
        }
    }
    break;
};

code returns

的第二个结果

我想要做的是将第一个实例中的值插入到 newInspection.rating 中,并使用适当的值而不是空值。

0 个答案:

没有答案