Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 嵌套承诺的变量赋值未解析(条带API)_Javascript_Promise_Stripe Payments_Javascript Objects - Fatal编程技术网

Javascript 嵌套承诺的变量赋值未解析(条带API)

Javascript 嵌套承诺的变量赋值未解析(条带API),javascript,promise,stripe-payments,javascript-objects,Javascript,Promise,Stripe Payments,Javascript Objects,使用Stripe API,我调用所有产品Stripe.products.list(),然后尝试使用map()函数为每个产品追加定价数据Stripe.prices.list()。每个调用都返回一个承诺,尽管嵌套的承诺没有解析,而是返回一个空的prices对象。我错过了什么 export function handler(event, context, callback) { stripe.products .list() .then(products =&g

使用Stripe API,我调用所有产品
Stripe.products.list()
,然后尝试使用
map()
函数为每个产品追加定价数据
Stripe.prices.list()
。每个调用都返回一个承诺,尽管嵌套的承诺没有解析,而是返回一个空的prices对象。我错过了什么

export function handler(event, context, callback) {
    stripe.products
        .list()
        .then(products =>
            products.data.map(product => ({
                ...product,
                prices: stripe.prices                    //  <-- ASSIGNMENT NOT RESOLVED
                    .list({ product: product.id })
                    .then(prices => {
                        console.log(prices)              //  <-- RESOLVED IN CONSOLE
                        return prices
                    }),
            }))
        )
        .then(rsp => {
            callback(null, {
                headers: { 'Content-Type': 'application/json' },
                statusCode: 200,
                body: JSON.stringify(rsp),
            })
        })
        .catch(err => console.warn(err))
}
导出函数处理程序(事件、上下文、回调){
条纹产品
.list()
。然后(产品=>
products.data.map(product=>({
…产品,
价格:stripe.prices//{
console.log(价格)//{
回调(null{
标题:{'Content-Type':'application/json'},
状态代码:200,
正文:JSON.stringify(rsp),
})
})
.catch(err=>console.warn(err))
}

Array.map
是同步执行的,您不需要等待
products.data.map
中的承诺。
prices
对象不是空的,它是一个承诺。
只有在处理程序返回后才会生成控制台中的输出

一种简单的方法是,将收到的产品存储在一个变量中。然后使用获取所有产品的价格。然后将这两个数组合并以获得最终结果

export function handler(event, context, callback) { 
  let theproducts;
  stripe.products
    .list()
    .then(products => {
      theproducts = products.map(p => p.data);
      return Promise.all(products.data.map(product => 
          stripe.prices.list({ product: product.id }))
        )
     })
     .then(prices => theproducts.map((p, i) => ({...p, prices: prices[i]})
     .then(rsp => {
       callback(null, {
         headers: { 'Content-Type': 'application/json' },
         statusCode: 200,
         body: JSON.stringify(rsp),
       })
      })
      .catch(err => console.warn(err))
}

您还可以在
stripe.prices.list()时立即合并产品和价格
解析,因此节省了对map的额外迭代。但我个人觉得这样更可读。如果没有太多的产品,额外的迭代不会花费太长时间。

数组。map
是同步执行的,您不必等待
products.data.map
中的承诺。
价格
对象不是空的,它是一个承诺。控制台中的输出仅在处理程序返回后生成

一种简单的方法是,将收到的产品存储在一个变量中。然后使用获取所有产品的价格。然后将这两个数组合并以获得最终结果

export function handler(event, context, callback) { 
  let theproducts;
  stripe.products
    .list()
    .then(products => {
      theproducts = products.map(p => p.data);
      return Promise.all(products.data.map(product => 
          stripe.prices.list({ product: product.id }))
        )
     })
     .then(prices => theproducts.map((p, i) => ({...p, prices: prices[i]})
     .then(rsp => {
       callback(null, {
         headers: { 'Content-Type': 'application/json' },
         statusCode: 200,
         body: JSON.stringify(rsp),
       })
      })
      .catch(err => console.warn(err))
}

您还可以在
stripe.prices.list()时立即合并产品和价格
解析,因此节省了对映射的额外迭代。但我个人觉得这样更可读。如果您没有太多的产品,额外的迭代不会花费太长时间。

您不需要等待内部承诺来解析:

export function handler(event, context, callback) {
    stripe.products
        .list()
        .then(products =>
            return Promise.all(products.data.map(product => {
                return stripe.prices                    //  <-- ASSIGNMENT NOT RESOLVED
                    .list({ product: product.id })
                    .then(prices => {
                        console.log(prices)              //  <-- RESOLVED IN CONSOLE
                        return prices
                    }),
            }))
            .then(prices => {
                return {
                    ...product,
                    prices
                };
            });
        )
        .then(rsp => {
            callback(null, {
                headers: { 'Content-Type': 'application/json' },
                statusCode: 200,
                body: JSON.stringify(rsp),
            })
        })
        .catch(err => console.warn(err))
}
导出函数处理程序(事件、上下文、回调){
条纹产品
.list()
。然后(产品=>
return Promise.all(products.data.map)(产品=>{
返回条带。价格//{
console.log(价格)//{
返回{
…产品,
价格
};
});
)
。然后(rsp=>{
回调(null{
标题:{'Content-Type':'application/json'},
状态代码:200,
正文:JSON.stringify(rsp),
})
})
.catch(err=>console.warn(err))
}

您不是在等待内部承诺解决:

export function handler(event, context, callback) {
    stripe.products
        .list()
        .then(products =>
            return Promise.all(products.data.map(product => {
                return stripe.prices                    //  <-- ASSIGNMENT NOT RESOLVED
                    .list({ product: product.id })
                    .then(prices => {
                        console.log(prices)              //  <-- RESOLVED IN CONSOLE
                        return prices
                    }),
            }))
            .then(prices => {
                return {
                    ...product,
                    prices
                };
            });
        )
        .then(rsp => {
            callback(null, {
                headers: { 'Content-Type': 'application/json' },
                statusCode: 200,
                body: JSON.stringify(rsp),
            })
        })
        .catch(err => console.warn(err))
}
导出函数处理程序(事件、上下文、回调){
条纹产品
.list()
。然后(产品=>
return Promise.all(products.data.map)(产品=>{
返回条带。价格//{
console.log(价格)//{
返回{
…产品,
价格
};
});
)
。然后(rsp=>{
回调(null{
标题:{'Content-Type':'application/json'},
状态代码:200,
正文:JSON.stringify(rsp),
})
})
.catch(err=>console.warn(err))
}

您已返回products.data.map中的数组。但是,这是一个承诺数组。当您映射它们并链接承诺时,它实际上不会解析承诺。我建议您尝试一些异步语法来简化代码

(async () => {
       const blah = products.data.map(product => {
            const prices = await stripe.prices.list({ product: product.id });
            return { ...product, prices };
        });
       console.log(blah);
})()

您已返回products.data.map中的数组。但是,这是一个承诺数组。当您映射它们并链接承诺时,它实际上不会解析承诺。我建议您尝试一些异步语法来简化代码

(async () => {
       const blah = products.data.map(product => {
            const prices = await stripe.prices.list({ product: product.id });
            return { ...product, prices };
        });
       console.log(blah);
})()

通过大量有用的输入,我最终得出以下结论

export function handler(event, context, callback) {
    stripe.products
        .list()
        .then(products => {
            return Promise.all(
                products.data.map(product =>
                    stripe.prices
                        .list({ product: product.id })
                        .then(prices => {
                            return prices
                        })
                        .then(prices => {
                            return {
                                ...product,
                                prices,
                            }
                        })
                )
            )
        })
        .then(rsp => {
            callback(null, {
                headers: { 'Content-Type': 'application/json' },
                statusCode: 200,
                body: JSON.stringify(rsp),
            })
        })
        .catch(err => console.warn(err))
}

通过大量有用的输入,我最终得出以下结论

export function handler(event, context, callback) {
    stripe.products
        .list()
        .then(products => {
            return Promise.all(
                products.data.map(product =>
                    stripe.prices
                        .list({ product: product.id })
                        .then(prices => {
                            return prices
                        })
                        .then(prices => {
                            return {
                                ...product,
                                prices,
                            }
                        })
                )
            )
        })
        .then(rsp => {
            callback(null, {
                headers: { 'Content-Type': 'application/json' },
                statusCode: 200,
                body: JSON.stringify(rsp),
            })
        })
        .catch(err => console.warn(err))
}

我知道你在做什么。我必须重新排列闭包。我知道你在做什么。我必须重新排列闭包。@derpirscher谢谢。我已经更新了我的答案。我只想给异步语法一个选项,直到它不起作用,因为
map
的回调不是
async
函数(只有匿名包装器是)您只能在
async
函数中使用
wait
,即使您将其定义为async,
Array.map
始终同步执行,因此它不会等待回调解决,而是返回一个承诺数组。唯一的方法是使用
Promise.all
for(让p进入products.data)
async
函数中循环。@derpirscher谢谢。我已经更新了我的答案。只想将async语法作为选项直到无法工作,因为
map
的回调不是
async
函数(只有匿名包装器是)您只能在
async
函数中使用
wait
,即使您将其定义为async,
Array.map
始终同步执行,因此它不会等待回调解决,而是返回一个承诺数组。唯一的方法是使用
Promise.all
for(让p进来)