3

I fetched the records using

find().toArray()

query. In that records, there is relation id of other document(table). I want to get records of relation table of each record of above find query result. Like:

   db.collection('serviceBooking').find({'request_to_sp_user_id': docs._id.toString()}).toArray(function (err, serviceBookingDocs) {
        if (serviceBookingDocs.length) {
            var asyncCalls = [];
            serviceBookingDocs.forEach(function (bookingRecord, key) {
                var temp = {};
                temp.userDetails = {};
                //Async call for getting the user details for all users
                asyncCalls.push(function (callback) {
                    db.collection('userDetails').findOne({'user_id': new mongo.ObjectID(bookingRecord.booked_by_user_id)}, function (err, userDetailsDocs) {
                        db.collection('serviceBookingDetails').find({'serviceBookingId': bookingRecord._id.toString()}).toArray(function (err, bookingDetailsDocs) {
                            if (userDetailsDocs) {
                                if (bookingDetailsDocs.length) {
                                    temp.bookingDetails = bookingDetailsDocs;
                                    bookingDetailsDocs.forEach(function (bookDetailItems, key) {
                                        db.collection('serviceCatalog').findOne({'_id': new mongo.ObjectID(bookDetailItems.catalogId), isDeleted: 0}, function (err, spCatalogs) {
                                            db.collection('spServiceCatalog').findOne({'_id': new mongo.ObjectID(spCatalogs.serviceCategory)}, function (err, spServiceCatalogDocs) {
                                                if (spCatalogs) {
                                                    (spServiceCatalogDocs)
                                                    spCatalogs.catalogName = spServiceCatalogDocs.name;
                                                    temp.bookingDetails[key].serviceCatalgs = spCatalogs;
                                                } else {
                                                    spCatalogs.catalogName = null;
                                                    temp.bookingDetails[key].serviceCatalgs = spCatalogs;
                                                }
                                                    callback(null, temp);
                                            })
                                        })
                                    })
                                }
                            } else {
                                callback(null, null);
                            }
                        })
                    })
                })
            })
        }
    })

I tried with callback function but it not get the values of category name from mainCategory document. I also tried to get the internal fetched category name outside the forEach() but its not getting in result in temp array.

0

1 Answer 1

0

This may help you.

It says.. Functions are the only thing on javascript that "enclose" scope.

This means that the variable items in your inner callback function are not accessible on the outer scope.

You can define a variable in the outer scope so it will be visible to all the inner ones:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.