0

The first code is the result I want the console log to return:

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

But the issue with this code is that it's not in the scope of the function I need it to be.

The result of the first code is:

First code result.

The second code which is in the scope I want, doesn't work:

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

The second result of code returns

What I want to do is to get the value like in the first instance to be inserted into the newInspection.rating with the appropriate value instead of a blank value.

Techius
  • 9
  • 5
  • Not an answer to your question, but you should *really* try to break this code down into a few functions. This level of nesting makes it very difficult to tell what's going on. It's far too much code to process all at once. You'd be surprised at how often I break down a complex chunk of code, and answers to problems suddenly become much clearer. – Carcigenicate May 18 '18 at 12:12
  • thanks for 4 space tab indent – David May 18 '18 at 12:30

0 Answers0