简体   繁体   English

节点js异步mongodb find()查询多个调用

[英]Node js asynchronous mongodb find() query multiple calls

I fetched the records using 我使用了获取记录

find().toArray() 找到()。指定者()

query. 查询。 In that records, there is relation id of other document(table). 在该记录中,存在其他文档(表)的关系id。 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. 我尝试使用回调函数,但它没有从mainCategory文档中获取类别名称的值。 I also tried to get the internal fetched category name outside the forEach() but its not getting in result in temp array. 我还尝试在forEach()之外获取内部获取的类别名称,但它没有获得临时数组的结果。

This may help you. 这可能对你有所帮助。

It says.. Functions are the only thing on javascript that "enclose" scope. 它说..函数是javascript上唯一“封闭”范围的东西。

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: 您可以在外部作用域中定义一个变量,以便所有内部变量都可以看到它:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM