从 Promise.all 获取空值
Getting null in values from Promise.all
我正在使用承诺。这是我的问题 here
的延续
我遇到的问题是作为响应,即对象数组具有空值。我会尝试解释这个
- 首先我得到了userId
- 通过userId获取用户心愿单产品
- 然后使用 userId 我得到 stores/shop list
- 然后我遍历商店列表并调用另一个 API 来检查这家商店是否是用户最喜欢的商店。
然后我得到每个商店的产品并追加一个对象和return。
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()
}
我得到的回复是
[{
"id": 1001,
"name": "Gaurdian Store",
"is_fave_shop": "0",
"products": [{
"id": 14285912,
"name": "Sofra Cream",
"is_wishlist": false
}]
},
null,
null,
{
"id": 1002,
"name": "decolite",
"is_fave_shop": "1",
"products": [{
"id": 14285912,
"name": "Denca SPF",
"is_wishlist": false
}]
}
]
实际商店将以 4 的形式出现,但会附加 null 而不是它。我在这里使用 Promises 有什么问题。
你调试过你的代码吗?
我会调试 Chrome 并在代码上添加一些断点以查看实际响应是什么。
这似乎与您的 .catch(() => {})
和 .catch(err => console.error(err))
调用有关。如果循环中的一个承诺有错误,它将被转换为一个 undefined
值(之前可以选择报告),因此 Promise.all
将用一个可能包含 undefined
值的数组来实现.如果你 JSON.stringify
那,你将在这些索引处得到 null
。
删除不执行任何操作的 .catch(() => {})
语句(或用日志记录替换它们),并检查您的错误日志。
我正在使用承诺。这是我的问题 here
的延续我遇到的问题是作为响应,即对象数组具有空值。我会尝试解释这个
- 首先我得到了userId
- 通过userId获取用户心愿单产品
- 然后使用 userId 我得到 stores/shop list
- 然后我遍历商店列表并调用另一个 API 来检查这家商店是否是用户最喜欢的商店。
然后我得到每个商店的产品并追加一个对象和return。
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() }
我得到的回复是
[{
"id": 1001,
"name": "Gaurdian Store",
"is_fave_shop": "0",
"products": [{
"id": 14285912,
"name": "Sofra Cream",
"is_wishlist": false
}]
},
null,
null,
{
"id": 1002,
"name": "decolite",
"is_fave_shop": "1",
"products": [{
"id": 14285912,
"name": "Denca SPF",
"is_wishlist": false
}]
}
]
实际商店将以 4 的形式出现,但会附加 null 而不是它。我在这里使用 Promises 有什么问题。
你调试过你的代码吗? 我会调试 Chrome 并在代码上添加一些断点以查看实际响应是什么。
这似乎与您的 .catch(() => {})
和 .catch(err => console.error(err))
调用有关。如果循环中的一个承诺有错误,它将被转换为一个 undefined
值(之前可以选择报告),因此 Promise.all
将用一个可能包含 undefined
值的数组来实现.如果你 JSON.stringify
那,你将在这些索引处得到 null
。
删除不执行任何操作的 .catch(() => {})
语句(或用日志记录替换它们),并检查您的错误日志。