NodeJS Callback没有在for循环外返回完成的数组

时间:2018-02-11 23:27:06

标签: node.js amazon-web-services callback aws-lambda aws-sdk

我有一个空的对象数组"存储"我现在正在嵌套for循环中推送对象。这个问题是当我在for循环外调用回调(null,存储)时,它返回一个空数组。我应该开一个承诺电话吗?不知道从哪里开始

var params = {
    TableName: USER_TABLE,
    KeyConditions: {
        orgid: {
            ComparisonOperator: 'EQ',
            AttributeValueList: [org_uuid]
        },
        orgkey: {
            ComparisonOperator: 'EQ',
            AttributeValueList: [secret_key]
        }
    }
};

// query table for devices tied to user
dynamodb.query(params, function(err, data) {
    if (err) {
        badRequest();
    } else {
        // check whether correct org_uuid & org_key has been passed
        var checked_org = data.Items[0].org_key;
        if (checked_org == secret_key) {
    var storing = [];
            var value = data.Items[0].read.values;
            for (var x = 0; x < value.length; x++) {
                var query_params = {
                    TableName: DEVICE_TABLE,
                    KeyConditions: {
                        device_id: {
                            AttributeValueList: {
                                S: value[x]
                            },
                            ComparisonOperator: 'EQ'
                        }
                    }
                };
                dynamodb.query(query_params, function(err, data) {
                    if (err) {
                        console.log(err);
                    } else {
                        for (var i = 0; i < data.Count; i++) {
                            storing.push(data.Items[i].device_id);
                        }
                    }
                });

            }
        callback(null, storing)
        }
    }
});

}

0 个答案:

没有答案