Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
he-yf opened this issue May 27, 2022 · 4 comments
Open

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

he-yf opened this issue May 27, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@he-yf
Copy link

he-yf commented May 27, 2022

问题简述
应用后拦截了所有接口请求且获取不到数据,返回:
[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 he-yf added the bug Something isn't working label May 27, 2022
@he-yf
Copy link
Author

he-yf commented May 27, 2022

完整报错信息:
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)

@lavyun
Copy link
Owner

lavyun commented May 28, 2022

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

@lavyun
Copy link
Owner

lavyun commented May 28, 2022

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

@he-yf
Copy link
Author

he-yf commented May 28, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants