我有一个json文件并创建了node.js服务器来设置端点,然后通过我的React Native应用程序获取此数据。如果我没有记错的话,它在星期五可以正常工作,但是我不得不弄乱一些东西,现在我完全不知道如何解决它。每当我收到错误消息:
可能的未处理的承诺拒绝(标识:0):TypeError:网络 请求失败 self.fetch / http://192.168.1.39:8081 / 1c49a23b-7fbb-c640-a946-c1e001192c92:27859:18 dispatchEvent @ blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:29144:13 setReadyState @ blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:28897:15 __didCompleteResponse @ blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:28724:11 发送/ <@ blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:28834:18 发射@blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:4538:15 __callFunction @ blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:2608:22 callFunctionReturnFlushedQueue / <@ blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:2385:11 __guard @ blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:2561:13 callFunctionReturnFlushedQueue @ blob:http://192.168.1.39:8081/1c49a23b-7fbb-c640-a946-c1e001192c92:2384:9 onmessage @ http://192.168.1.39:8081/debugger-ui/debuggerWorker.js:72:25
我的节点服务器:
const filename = './logos.json';
const server = http.createServer((req, res) => {
if (req.url === "/logo") {
res.writeHead(200, { "Content-Type": "application/json" });
fs.createReadStream(__dirname + "/logos.json").pipe(res)
}
})
server.listen(3000, (err) => {
if (err) throw err;
console.log('server is listening on port 3000');
})
和我的RN代码:
syncLogoData = () => {
fetch('http://localhost:3000/logo')
.then(resp => resp.json())
.then(data => console.log(data))
.catch(err => console.log(err))
}
答案 0 :(得分:1)
好像您正在尝试在设备上运行它。设备不知道localhost
指向您的服务器。在您的syncLogoData
中,将uri更改为http://ip_address:3000/logo
,它应该可以正常工作。
在计算机浏览器中打开http://localhost:3000/logo
来确保服务器代码正确也是有帮助的。