我有代碼類別Action.jsimport {FETCH_CATEGORIES_START, FETCH_CATEGORIES_SUCCESS, FETCH_CATEGORIES_ERROR} from "./actions-types/categories-actions"import axios from "axios"export function fetchCategories() { return async dispatch => { dispatch(fetchCategoriesStart()) try { const response = await axios.get('/api/category/categories') const categories = [] categories.push(response.data.data) dispatch(fetchCategoriesSuccess(categories)) } catch (e) { dispatch(fetchCategoriesError(e)) } }}export function fetchCategoriesStart() { return{ type: FETCH_CATEGORIES_START }}export function fetchCategoriesSuccess(categories) { return{ type: FETCH_CATEGORIES_SUCCESS, categories }}export function fetchCategoriesError(error) { return{ type: FETCH_CATEGORIES_ERROR , error }}及減速機(jī)代碼import {FETCH_CATEGORIES_START, FETCH_CATEGORIES_SUCCESS, FETCH_CATEGORIES_ERROR} from "../actions/actions-types/categories-actions"const initialState = { categories: [], loading: false, error: null}export default function categoriesReducer(state = initialState, action){ switch (action.type) { case FETCH_CATEGORIES_START: return { ...state, loading: true } case FETCH_CATEGORIES_SUCCESS: return { ...state, loading: false, categories: action.categories[0] } case FETCH_CATEGORIES_ERROR: return { ...state, loading: false, error: action.error } default: return state }}但是當(dāng)我獲得類別時(shí),我需要按類別 ID 產(chǎn)品進(jìn)行額外搜索并放入對象類別(例如帶有產(chǎn)品數(shù)組的關(guān)鍵產(chǎn)品),即發(fā)出另一個(gè)異步請求。但我無法在異步函數(shù)中進(jìn)行異步。const response = await axios.get('/api/category/categories')const categories = []categories.push(response.data.data)也許我提出的問題不對,但我需要下一個(gè)邏輯。為類別制作 axios.get獲取類別當(dāng)數(shù)組產(chǎn)品放入對象類別中時(shí),通過類別 ID 和響應(yīng)創(chuàng)建新的 axios.get 謝謝
如何在函數(shù)中添加通過id搜索產(chǎn)品?
GCT1015
2023-10-20 16:41:12