Javascript 发生404时句柄承诺不解析
我是新来的 我有一个vuejs组件,其fetch()函数类似于:Javascript 发生404时句柄承诺不解析,javascript,es6-promise,Javascript,Es6 Promise,我是新来的 我有一个vuejs组件,其fetch()函数类似于: async fetch () { this.loadingDetails = true const products = await Promise.all(this.items.map((item) => { try { const products = this.$wizaplace.get('/catalog/products/' + item.declinationId.slice(0, 4), {
async fetch () {
this.loadingDetails = true
const products = await Promise.all(this.items.map((item) => {
try {
const products = this.$wizaplace.get('/catalog/products/' + item.declinationId.slice(0, 4), { // When the ID is incorrect, a 404 is returned
lang: this.$i18n.locale
})
console.log(products)
} catch (e) {
// eslint-disable-next-line no-console
console.log('Error Fetching profile: ', e)
}
return products
}))
console.log(products)
this.productsDetails = products.map((product) => {
console.log(product)
const generalAttributes = product.attributes.find(attr => attr.id === 49)
const brewerie = generalAttributes.children.find(attr => attr.id === 59)
const color = generalAttributes.children.find(attr => attr.id === 15)
// console.log(product)
return {
attributes: product.attributes,
brewerie: (brewerie && brewerie.value[0]) ? brewerie.value[0] : '',
type: generalAttributes.children.find(attr => attr.id === 47).value[0],
color: (color && color.value[0]) ? color.value[0] : ''
}
})
this.loadingDetails = false
// console.log(this.productsDetails[0])
},
当Promise.All(回调)中的一个HTTP调用失败时,Promise将无法解析。请问我该怎么办
谢谢你的帮助 您退回的产品不在试用区内 你必须改变这一点
const products = await Promise.all(this.items.map((item) => {
try {
const products = ...
// ...
} catch() {
// ...
}
return products
}))
有了这个
const products = await Promise.all(this.items.map((item) => {
try {
const products = ...
// ...
return products
} catch() {
// ...
}
}))
您正在执行
退货产品内部const products=wait Promise.all
?你以后一定要买吗?哦,你还有一个const-products
里面的const-products
。。。但是它的作用域在try
块中,因此当您返回产品时,实际上返回的是外部作用域,而不是try
作用域。。。实际上,你应该为变量使用不同的名称,你的代码会有点清晰,什么是$wizaplace.get
,你能给我们展示一下它的代码吗?当http调用失败时,您是否收到“错误获取配置文件”日志消息?