在 Node.js 中对字典调用 forEach

Calling forEach on Dictionary in Node.js

我正在尝试遍历 Firebase 引用。它有效,但由于某种原因,forEach 循环比引用中的对象多运行一次。这会导致 Promise.all() 函数失败,整个 promise 也会失败。这是我的代码。我不知道我做错了什么。

return userReceiptMetrics.child(userID).child('postItemIDs').orderByChild('itemID').equalTo(oldProductID).once('value', function(oldSnapshot) {
            var oldPostItemIDs = [];
            var metrics = oldSnapshot.val()

            if (oldSnapshot.val() != null) {
                return Promise.all(oldSnapshot.forEach(function(record) {
                    console.log(record.val());
                    var oldKey = record.key;
                    var newKey = oldKey.replace(oldProductID, newProductID);
                    var data = record.val();
                    oldPostItemIDs.push(oldKey);
                    data.itemID = newProductID;
                    updateObject['userReceiptMetrics/'+userID+'/postItemIDs/'+newKey] = data;
                    updateObject['userReceiptMetrics/'+userID+'/postItemIDs/'+oldKey] = null;
                })).then(function() {
                    return Promise.all(oldPostItemIDs.map(function(oldPostItemID) {
                        return userReceiptMetrics.child(userID).child('postItems').child(oldPostItemID).then(function(oldPostItem) {
                            var oldKey = oldPostItem.key
                            var newKey = oldKey.replace(oldProductID, newProductID)
                            var data = record.val()                        
                            updateObject['userReceiptMetrics/'+userID+'/postItems/'+newKey] = data;
                            updateObject['userReceiptMetrics/'+userID+'/postItems/'+oldKey] = null;

                            progress(38);
                        });
                    }))
                }).catch(function(error) {
                    console.log('fudge louise');
                });
            }
        });

控制台输出如下:

 App listening on port 8080
Press Ctrl+C to quit.
FIREBASE WARNING: Using an unspecified index. Consider adding ".indexOn": "_state" at /productUpdateQueue/tasks to your security rules for better performance 
FIREBASE WARNING: Using an unspecified index. Consider adding ".indexOn": "_state" at /productUpdateQueue/tasks to your security rules for better performance 
{ date: '2016-12-21 22:05:03',
  itemID: 'Macys-EReceipts-MENS HOSIERY-Size(No size provided)-Color(No color provided)-786888403743',
  postID: '-KZbmaThvxNmrvHwh_mc' }
{ date: '2016-12-21 22:05:03',
  itemID: 'Macys-EReceipts-MENS HOSIERY-Size(No size provided)-Color(No color provided)-786888403743',
  postID: '-KZbxAUcwzcP28C91EZA' }
FIREBASE WARNING: Using an unspecified index. Consider adding ".indexOn": "itemID" at /userReceiptMetrics/HeQST8hSkoPUmkBiVDR0tpSPo0x2/postItemIDs to your security rules for better performance 
fudge louise

所以看起来 Promise.all() 调用失败了,因为对象列表末尾有一个空对象。这是更正后的代码。

return userReceiptMetrics.child(userID).child('postItemIDs').orderByChild('itemID').equalTo(oldProductID).once('value', function(oldSnapshot) {
            var oldPostItemIDs = [];
            var metrics = oldSnapshot.val()

            if (oldSnapshot.val() != null) {
                return Promise.all(oldSnapshot.forEach(function(record) {
                    console.log(record.val());
                    var oldKey = record.key;
                    var newKey = oldKey.replace(oldProductID, newProductID);
                    var data = record.val();
                    oldPostItemIDs.push(oldKey);
                    data.itemID = newProductID;
                    updateObject['userReceiptMetrics/'+userID+'/postItemIDs/'+newKey] = data;
                    updateObject['userReceiptMetrics/'+userID+'/postItemIDs/'+oldKey] = null;
                })).then(function() {
                    return oldPostItemIDs.map(function(oldPostItemID) {
                        return userReceiptMetrics.child(userID).child('postItems').child(oldPostItemID).then(function(oldPostItem) {
                            var oldKey = oldPostItem.key
                            var newKey = oldKey.replace(oldProductID, newProductID)
                            var data = record.val()                        
                            updateObject['userReceiptMetrics/'+userID+'/postItems/'+newKey] = data;
                            updateObject['userReceiptMetrics/'+userID+'/postItems/'+oldKey] = null;

                            progress(38);
                        });
                    })
                }).catch(function(error) {
                    console.log('fudge louise');
                });
            }
        });