从Promise.all获取值为null

时间:2017-03-09 16:03:56

标签: javascript node.js promise ecmascript-5

我正在使用承诺。这是我的问题here

的延续

我遇到的问题是响应,即一个对象数组具有空值。我会尝试解释这个

  1. 首先我得到userId
  2. 从userId获取用户whishlist产品
  3. 然后使用userId我得到商店/商店列表
  4. 然后我遍历商店列表并调用另一个API来检查这个商店是否是用户最喜欢的商店。
  5. 然后我获取每个商店的产品并附加到一个对象中并返回。

     int ReadScores(string fileName, string names[], float avg_scores[], int array_size){
    
    int linesCounted = 0;
    float indScores[array_size];
    
    int lineCounter = 0;
    string myLine, nameSubString, scoreSubString;
    float scoreConvert = 0.0;
    float averageScores = 0.0;
    
    ifstream myFileIn;
    //open the file
    myFileIn.open(fileName, ios::in);
        if (myFileIn.fail()){
            cout << "Error opening "<< fileName << endl;
            return 0;
        }
        int index = 0;
        //read the file until the end of file is reached
        while (getline(myFileIn, myLine)){
                //firstComma will hold the integer value of the index position of the first comma found
                int firstComma = myLine.find(',');
                 //this should grab the the names at the beginning of each string on each new line
                nameSubString = myLine.substr(0, firstComma);
                names[index] = nameSubString;
    
    
                int startingPos = 0;
                float commaCounter = 0;
                //find how many commas are in a string and use that to limit your loop
                for (int ind = 0; ind < myLine.length(); ind++){
                    if (myLine[ind] == ',')
                        commaCounter++;
                    }
    
                for (int ind = 0; ind < commaCounter; ind++){
                         //grab the first number and store it the scoreSubString variable
                        int found = myLine.find(',', firstComma) + 1;
                        scoreSubString = myLine.substr(found, myLine.find(',', found));
                        //change the value of firstComma to the next index location
                        firstComma = found + 1;
    
                        ///convert string to number
                        stringstream(scoreSubString) >> scoreConvert;
                        ///store number in float array
                        indScores[ind] = scoreConvert;
                    }
    
            averageScores = 0;
                for (int ind = 0; ind < commaCounter; ind++){
                    averageScores = indScores[ind] + averageScores;
                    }
                    float averageOverall = averageScores/commaCounter;
                    avg_scores[index] = averageOverall;
            index++;
            if (!myLine.empty()){
                    lineCounter++;
                }
            }
        myFileIn.close();
    
  6. 我得到的回应是

    function getStoresList(context) {
        const userID = common.getUserID(context)
        let userWishListProd = []
    
        return userID
            .then(uid => Wishlist.getUserWishlistProducts(uid).then((products) => {
                userWishListProd = products.data.map(product => +product.id)
                return uid
            }))
            .then(uid => api.getOfficialStoresList(uid).then((response) => {
                    if (!response.data) {
                        const raw = JSON.stringify(response)
                        return []
                    }
    
                    const shops = response.data
                    return Promise.all(
                            shops.map((shop) => {
                                const id = shop.shop_id
                                const shopobj = {
                                    id,
                                    name: shop.shop_name,
                                }
                                return favAPI.checkFavourite(uid, id)
                                    .then((favData) => {
                                        shopobj.is_fave_shop = favData
    
                                        // Fetch the products of shop
                                        return getProductList(id, uid)
                                            .then((responsedata) => {
                                                shopobj.products = responsedata.data.products.map(product => ({
                                                    id: product.id,
                                                    name: product.name,
                                                    is_wishlist: userWishListProd.indexOf(product.id) > -1,
                                                }))
                                                return shopobj
                                            })
                                            .catch(() => {})
                                    })
                                    .catch(err => console.error(err))
                            }))
                        .then(responses => responses)
                        .catch(err => console.log(err))
                })
                .catch(() => {}))
            .catch()
    }
    

    实际商店的数量是4,但是不会追加null。我在这里做Promise有什么问题。

2 个答案:

答案 0 :(得分:0)

您是否调试了代码? 我会在Chrome上调试并在代码上添加一些断点,以查看实际响应是什么。

答案 1 :(得分:0)

这似乎与您的.catch(() => {}).catch(err => console.error(err))调用有关。如果循环中的一个promise有错误,它将转换为undefined值(可选择在之前报告),以便Promise.all符合可能包含undefined值的数组。如果您JSON.stringify,那么您将获得null这些索引。

删除无效的.catch(() => {})语句(或将其替换为日志记录),并检查错误日志。