javascript - 破坏的麻烦

有一个if条件


结果console.log他们什么也没打印,并出现在浏览器控制台 TypeError: Cannot read property 'to' of undefined 上


instance.interceptors.response.use(

        (res) => {

            const { pathname } = window.location;

            const isRedirect = res.status === HTTP_TEMPORARY_REDIRECT;


            const data = { isRedirect, data: res.data };


            if (isRedirect) {

                console.log(`pathname: ${pathname}`);

                console.log(`isRedirect: ${isRedirect}`);

                const { redirectDataSource: { to: redirectRoute, ...redirectData } } 

                const {

                    redirectDataSource: { to: redirectRoute, ...redirectData },

                } = res.data.error || res.data;

                console.log(`redirectRoute: ${redirectRoute}`);

                console.log(`redirectDataSource: ${redirectDataSource}`);

            }


            return Promise.resolve(data);

        },

        ...


阿晨1998
浏览 85回答 1
1回答

POPMUISE

redirectDataSource如果未定义,您需要添加默认值let res = {  data: {    error: undefined,    redirectDataSource: undefined  }}const {  redirectDataSource: {    to: redirectRoute,  } = {to: 'defaultValue'}} = res.data.error || res.data;console.log(redirectRoute)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript