better-mock icon indicating copy to clipboard operation
better-mock copied to clipboard

react-native 拦截所有接口而非指定url

Open he-yf opened this issue 3 years ago • 4 comments
trafficstars

问题简述 应用后拦截了所有接口请求且获取不到数据,返回: [ReferenceError: Property 'location' doesn't exist]或者[ReferenceError: Property 'location' doesn't exist]

复现步骤 1、这是我的mock,js const Mock = require('better-mock');

const data = Mock.mock({ 'list|6-10': [ () => Mock.mock({ course_name: '课程名称', 'course_desc|20-100': '*', module_name: '模块', 'zsd_id|5-10': '1111', 'is_enable|1-2': true, 'degree|1-5': 1, // 难度 }), ], }); console.log(data);

Mock.mock(/api/v4/course/catalogue/, 'post', option => { console.log('mock---api/v4/course/catalogue请求参数', option); return data; });

2、这是我在根目录index.js的引用代码 // 引入mock let mock = true; if ((env === 'test' || env === 'dev') && mock) { require('./mock'); }

3、这是我的接口调用(post是封装好的带naseUrl的axios请求) function getCourseCatalogue({ user_id, course_id, class_id, }: { user_id: number; course_id: number; class_id: number; }) { return post('api/v4/course/catalogue', {user_id, course_id, class_id}, false); }

期望有小伙伴能帮助解决~

he-yf avatar May 27 '22 14:05 he-yf

完整报错信息: ReferenceError: Property 'location' doesn't exist at createNativeXHR (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:365621:42) at MockXMLHttpRequest (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:365408:31) at dispatchXhrRequest (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:133267:39) at tryCallTwo (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:28507:9) at doResolve (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:28671:25) at Promise (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:28530:14) at xhrAdapter (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:133258:23) at dispatchRequest (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:133105:19) at tryCallOne (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:28498:16) at anonymous (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:28599:27) at apply (native) at anonymous (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:29714:26) at _callTimer (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:29602:17) at _callImmediatesPass (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:29641:17) at callImmediates (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:29859:33) at __callImmediates (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:3387:35) at anonymous (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:3165:34) at __guard (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:3370:15) at flushedQueue (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:3164:21) at callFunctionReturnFlushedQueue (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.questionapp&modulesOnly=false&runModule=true:3146:33)

he-yf avatar May 27 '22 15:05 he-yf

目前并不支持在 react-native 环境下使用。

lavyun avatar May 28 '22 04:05 lavyun

如果要使用,可以本地起一个 node server,并在服务端构造 mock 数据

lavyun avatar May 28 '22 04:05 lavyun

好的,谢谢解答。

---- 回复的原邮件 ---- | 发件人 | @.> | | 日期 | 2022年05月28日 12:06 | | 收件人 | @.> | | 抄送至 | @.@.> | | 主题 | Re: [lavyun/better-mock] react-native 拦截所有接口而非指定url (Issue #89) |

如果要使用,可以本地起一个 node server,并在服务端构造 mock 数据

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

he-yf avatar May 28 '22 07:05 he-yf