Uncaught (in promise) TypeError: 无法解构 'err.response' 的属性 'data',因为它是未定义的

问题描述

我收到此错误

未捕获(承诺)类型错误:无法解构 'err.response' 的属性 'data',因为它未定义。

但我的数据成功插入到数据库中。它说我在前端的异步函数中收到该错误。但我没有看到任何错误

const onStartMatch = async (match) => {
        try {
            const liveMatch = {
                schedule: match._id,teamOne: match.teamOne,teamTwo: match.teamTwo,};
            const { data } = await fetchContext.authAxios.post(
                `facilitator/live-match/`,liveMatch
            );

            setSuccess(data.message);
            setError('');
            setopen(false);
            setopenAlert(false);

            if (match.gameEvent === 'basketball') {
                return history.push(`/basketball-scoreboard/${data.newLiveMatch._id}`);
            }
            if (match.gameEvent === 'volleyball') {
                return history.push(`/volleyball-scoreboard/${data.newLiveMatch._id}`);
            }
            if (match.gameEvent === 'soccer') {
                return history.push(`/soccer-scoreboard/${data.newLiveMatch._id}`);
            }
        } catch (err) {
            const { data } = err.response;
            setError(data.message);
            setSuccess('');
        }
    };

如您所见,我发布并获取新数据并将其设置为参数以转到另一个页面。 这是我在后端的代码

exports.livematch = async (req,res) => {
    try {
        const { sub,gameEvent } = req.user;
        const { teamOne,teamTwo,schedule } = req.body;

        const liveMatchData = {
            schedule: schedule,teamOne: teamOne,teamTwo: teamTwo,isstarted: true,};

        const input = Object.assign({},liveMatchData,{
            user: sub,gameEvent: gameEvent,});

        const newLiveMatch = new LiveMatch(input);
        await newLiveMatch.save();
        res.status(201).json({
            message: 'A new match has been created!',newLiveMatch,});
    } catch (err) {
        console.log(err);
        return res.status(400).send({
            message: 'There was a problem creating a live match',});
    }
};

我也使用 MongoDB 的更改流,但我认为它不会导致错误。 这是我在变更流中的代码

changeLiveMatchesstream.on('change',(liveMatch) => {
            console.log(liveMatch);
            if (liveMatch.operationType === 'insert') {
                const liveMatchDetails = liveMatch.fullDocument;
                pusher.trigger('liveMatch','inserted',{
                    _id: liveMatchDetails._id,schedule: liveMatchDetails.schedule,teamOne: liveMatchDetails.teamOne,teamTwo: liveMatchDetails.teamTwo,isDone: liveMatchDetails.isDone,gameEvent: liveMatchDetails.gameEvent,});
            }
            if (liveMatch.operationType === 'update') {
                const liveMatchDetails = liveMatch.documentKey;
                pusher.trigger('liveMatch','updated',});
            } else {
                console.log('Error triggering Pusher');
            }
        });

我使用 Pusher JS 作为实时获取库。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)