获取 undefined 或 Promise { <pending> } 试图推送到数组
Get undefined or Promise { <pending> } trying to push to an array
我在以下代码中推送到 SSR 页面中的数组时遇到问题。
let categories = []
categories = await axios.get(`http:/.../${price_list_name}`).then(res => {
return res.data
})
const child_categories = categories.related.category_childs.hits.hits
const childs_en_names = []
if (child_categories.length > 0) {
for (var doc in child_categories) {
childs_en_names.push(child_categories[doc]._source.en_name)
}
}
const get_products = async (en_name) => {
await axios.get(`http://.../${en_name}`).then(res => {
let data = {
"en_name": en_name,
"products": res.data.related.childs_products
}
return data
})
}
const products = await Promise.all(childs_en_names.map(en_name => get_products(en_name)))
// logging
// console.log(categories.products, 'props')
console.log(products, 'products')
cosole.log(products, 'products') returns me undefined or Promise { }.我已经搜索了很多,但没有成功。
任何帮助将不胜感激。
您没有从 get_products
返回任何内容。如果您也打算使用 then
,那么使用 async/await
毫无意义。这有点违背了重点。
const get_products = async (en_name) => {
const res = await axios.get(`http://.../${en_name}`);
const data = {
en_name: en_name,
products: res.data.related.childs_products
}
return data;
}
我在以下代码中推送到 SSR 页面中的数组时遇到问题。
let categories = []
categories = await axios.get(`http:/.../${price_list_name}`).then(res => {
return res.data
})
const child_categories = categories.related.category_childs.hits.hits
const childs_en_names = []
if (child_categories.length > 0) {
for (var doc in child_categories) {
childs_en_names.push(child_categories[doc]._source.en_name)
}
}
const get_products = async (en_name) => {
await axios.get(`http://.../${en_name}`).then(res => {
let data = {
"en_name": en_name,
"products": res.data.related.childs_products
}
return data
})
}
const products = await Promise.all(childs_en_names.map(en_name => get_products(en_name)))
// logging
// console.log(categories.products, 'props')
console.log(products, 'products')
cosole.log(products, 'products') returns me undefined or Promise { }.我已经搜索了很多,但没有成功。
任何帮助将不胜感激。
您没有从 get_products
返回任何内容。如果您也打算使用 then
,那么使用 async/await
毫无意义。这有点违背了重点。
const get_products = async (en_name) => {
const res = await axios.get(`http://.../${en_name}`);
const data = {
en_name: en_name,
products: res.data.related.childs_products
}
return data;
}