Rails server with React Native project getting error: Unhandled promise rejection: TypeError: Object is not a function
Rails server with React Native project getting error: Unhandled promise rejection: TypeError: Object is not a function
我是 Rails Ruby 的初学者。目前在 Rails 服务器上有一个 Ruby,我正试图与 React Native/Expo 应用程序进行交互。我正在使用 rails s
启动服务器,它似乎是 运行 以下输出
=> Booting Puma
=> Rails 5.2.4.6 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.6 (ruby 2.6.6-p146), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
在另一个终端 window 我正在使用以下命令启动 postgresql 数据库:pg_ctl -D /usr/local/var/postgres start
一切似乎都很好。
我正在对服务器进行示例调用。它似乎没有到达服务器,我得到以下输出:
[Unhandled promise rejection: TypeError: Object is not a function (near '...fetch...')]
at screens/rankingsPage/Rankings.jsx:28:22 in handlePress
at screens/rankingsPage/Rankings.jsx:28:22 in handlePress
at node_modules/react-native/Libraries/Pressability/Pressability.js:691:17 in _performTransitionSideEffects
at node_modules/react-native/Libraries/Pressability/Pressability.js:628:6 in _receiveSignal
at node_modules/react-native/Libraries/Pressability/Pressability.js:524:8 in responderEventHandlers.onResponderRelease
at [native code]:null in forEach
at [native code]:null in callFunctionReturnFlushedQueue
这是我的提取调用。有谁知道问题出在哪里以及我该如何解决?谢谢。
export const emailNewAccount = async (email, password, passwordConfirmation, username) => {
console.log("test call"); // <-- successfully logging
const url = `${serverUrl}/api/v1/create_account`; // <-- "http://localhost:3000/api/v1/create_account
const responseData = await fetch(url, {
headers: { "Content-Type": "application/json" },
method: "POST",
body: JSON.stringify(email, password, passwordConfirmation, username)
});
const json = await responseData.json();
console.log("json : ", json); // <-- not logging
return json;
};
好的!您正试图在那里记录 JSON 数据,这是不可能的。它必须是一个字符串。请尝试将代码更改为此
const jsonData = await responseData
const stringifiedData = JSON.stringify(jsonData)
console.log("json : ", stringifiedData);
事实证明,在服务器上工作的人使用 node-fetch
作为导入。
import * as fetch from "node-fetch";
所以当使用 fetch
时,我们使用 node-fetch
而不是使用原生 fetch
。当我们注释掉导入(或删除它)并简单地使用 React Native 原生的 fetch
时,它起作用了。
我是 Rails Ruby 的初学者。目前在 Rails 服务器上有一个 Ruby,我正试图与 React Native/Expo 应用程序进行交互。我正在使用 rails s
启动服务器,它似乎是 运行 以下输出
=> Booting Puma
=> Rails 5.2.4.6 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.6 (ruby 2.6.6-p146), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
在另一个终端 window 我正在使用以下命令启动 postgresql 数据库:pg_ctl -D /usr/local/var/postgres start
一切似乎都很好。
我正在对服务器进行示例调用。它似乎没有到达服务器,我得到以下输出:
[Unhandled promise rejection: TypeError: Object is not a function (near '...fetch...')]
at screens/rankingsPage/Rankings.jsx:28:22 in handlePress
at screens/rankingsPage/Rankings.jsx:28:22 in handlePress
at node_modules/react-native/Libraries/Pressability/Pressability.js:691:17 in _performTransitionSideEffects
at node_modules/react-native/Libraries/Pressability/Pressability.js:628:6 in _receiveSignal
at node_modules/react-native/Libraries/Pressability/Pressability.js:524:8 in responderEventHandlers.onResponderRelease
at [native code]:null in forEach
at [native code]:null in callFunctionReturnFlushedQueue
这是我的提取调用。有谁知道问题出在哪里以及我该如何解决?谢谢。
export const emailNewAccount = async (email, password, passwordConfirmation, username) => {
console.log("test call"); // <-- successfully logging
const url = `${serverUrl}/api/v1/create_account`; // <-- "http://localhost:3000/api/v1/create_account
const responseData = await fetch(url, {
headers: { "Content-Type": "application/json" },
method: "POST",
body: JSON.stringify(email, password, passwordConfirmation, username)
});
const json = await responseData.json();
console.log("json : ", json); // <-- not logging
return json;
};
好的!您正试图在那里记录 JSON 数据,这是不可能的。它必须是一个字符串。请尝试将代码更改为此
const jsonData = await responseData
const stringifiedData = JSON.stringify(jsonData)
console.log("json : ", stringifiedData);
事实证明,在服务器上工作的人使用 node-fetch
作为导入。
import * as fetch from "node-fetch";
所以当使用 fetch
时,我们使用 node-fetch
而不是使用原生 fetch
。当我们注释掉导入(或删除它)并简单地使用 React Native 原生的 fetch
时,它起作用了。