0

这是我的后端控制器,我正在获取房间数据

const allRooms = catchAsyncErrors(async (req, res) => {

    const resPerPage = 4;

    const roomsCount = await Room.countDocuments();

    const apiFeatures = new APIFeatures(Room.find(), req.query)
        .search()
        .filter()

    let rooms = await apiFeatures.query;
    let filteredRoomsCount = rooms.length;

    apiFeatures.pagination(resPerPage)
    rooms = await apiFeatures.query;

    res.status(200).json({
        success: true,
        roomsCount,
        resPerPage,
        filteredRoomsCount,
        rooms
    })

})

这是我正在发送有效负载和数据的 redux 操作

    export const getRooms = (req, currentPage = 1, location = '', guests, category) => async (dispatch) => {
    try {

        const { origin } = absoluteUrl(req);

        let link = `${origin}/api/rooms?page=${currentPage}&location=${location}`

        if (guests) link = link.concat(`&guestCapacity=${guests}`)
        if (category) link = link.concat(`&category=${category}`)

        const { data } = await axios.get(link)

        dispatch({
            type: ALL_ROOMS_SUCCESS,
            payload: data
        })

    } catch (error) {
        dispatch({
            type: ALL_ROOMS_FAIL,
            payload: error.response.data.message
        })
    }
}

这是我的减速器功能,调度房间数据

export const allRoomsReducer = (state = { rooms: [] }, action) => {
    switch (action.type) {

        case ALL_ROOMS_SUCCESS:
            return {
                rooms: action.payload.rooms
            }

        case ALL_ROOMS_FAIL:
            return {
                error: action.payload
            }

        case CLEAR_ERRORS:
            return {
                ...state,
                error: null
            }

        default:
            return state
    }
}

在这里,我想使用 wrapper.getStaticProps 获取数据,但出现错误,但是当我使用 wrapper.getServerSideProps 时,我得到了数据。

 export const getStaticProps = wrapper.getStaticProps(store=> async ({ req, query, })  => {
  await store.dispatch(getRooms(req, query.page, query.location, query.guests, query.category))
})
4

1 回答 1

0

似乎在({ req, query, }) =>queryundefined

根据 next的文档context对象 forgetStaticProps没有属性query。它仅在getServerSideProps.

另请参阅此信息

仅在构建时运行 因为 getStaticProps 在构建时运行,所以它不会接收仅在请求期间可用的数据,例如查询参数或 HTTP 标头,因为它会生成静态 HTML。

于 2021-09-09T11:32:18.833 回答