英文:
"FirebaseError: internal" when calling onCall cloud function from client code
问题
我有一个React Native - Expo应用程序,最近使用firebase-functions/v2/https的onCall()实现了一个基本的云函数,代码如下:
exports.addmessage = onCall((request) => {
logger.info("Request: " + request.data.text);
return { text: request.data.text };
});
我部署了这个函数,看起来部署似乎正确。然后,我尝试从我的客户端代码中的App组件调用这个函数,代码如下:
export default function App() {
const addmessage = httpsCallable(functions, 'addmessage');
function callFunction() {
addmessage({ text: "Hello world!" })
.then((result) => {
console.log("Function called");
console.log(result);
}).catch((error) => {
console.log("Function call failed");
console.log(error);
});
}
return (
<SafeAreaProvider>
<SafeAreaView style={styles.container}>
<Button title="Call cloud function" onPress={() => callFunction()}></Button>
<StatusBar style="auto" />
</SafeAreaView>
</SafeAreaProvider>
);
}
当我点击按钮调用云函数时,我得到了"Function call failed"和"[FirebaseError: internal]"的消息。这是否是调用可调用函数的正确方式?
我已经检查了firebase控制台中函数的日志记录,但没有错误记录在那里。我应该如何找到/修复这个问题?
我已经测试了onRequest函数,它按预期工作。
我在项目的package.json中使用"firebase": "9.22.1"作为依赖项(尽管我也测试过"firebase": "9.20.0")。
在我的functions的package.json中,我有以下依赖项:"firebase-admin": "^11.8.0","firebase-functions": "^4.3.1"。
英文:
I have a React Native - Expo application, and I recently implemented a basic cloud function using the firebase-functions/v2/https onCall() like so:
exports.addmessage = onCall((request) => {
logger.info("Request: " + request.data.text);
return {text: request.data.text}
});
I deploy this function, and it gets deployed seemingly correct. Then I try to call this function from my client code in the App - component like so:
export default function App() {
const addmessage = httpsCallable(functions, 'addmessage');
function callFunction() {
addmessage({ text: "Hello world!" })
.then((result) => {
console.log("Function called");
console.log(result);
}).catch((error) => {
console.log("Function call failed");
console.log(error)
});
}
return (
<SafeAreaProvider>
<SafeAreaView style={styles.container}>
<Button title="Call cloud function" onPress={() => callFunction()}></Button>
<StatusBar style="auto" />
</SafeAreaView>
</SafeAreaProvider>
);
}
When I click the button to call the cloud function, I get "Function call failed" and "[FirebaseError: internal]". Is this the correct way to call the callable function?
I have checked the logger for the function inside the firebase portal, but no errors are logged there. What can I do to find/fix this issue?
I have tested with an onRequest-function, and this works as expected.
I use "firebase": "9.22.1" as dependency in project-package.json (though I have also tested with "firebase": "9.20.0")
In my functions package.json, I have these dependencies: "firebase-admin": "^11.8.0", "firebase-functions": "^4.3.1".
答案1
得分: 1
I didn't try your Cloud Function but with
```javascript
exports.addmessage = onCall((request) => {
logger.info("Request: " + request.data.text);
return {text: text}
});
you actually don't declare the text
variable.
The following should do the trick:
exports.addmessage = onCall((request) => {
const text = request.data.text;
logger.info("Request: " + text);
return {text: text} // Or return { text }
});
I also advise to better manage the error logging in your Cloud Function, by following the documentation: In the CF and in the client.
<details>
<summary>英文:</summary>
I didn't try your Cloud Function but with
```javascript
exports.addmessage = onCall((request) => {
logger.info("Request: " + request.data.text);
return {text: text}
});
you actually don't declare the text
variable.
The following should do the trick:
exports.addmessage = onCall((request) => {
const text = request.data.text;
logger.info("Request: " + text);
return {text: text} // Or return { text }
});
I also advise to better manage the error logging in your Cloud Function, by following the documentation: In the CF and in the client.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论