简体   繁体   English

Firebase可调用函数响应不起作用

[英]Firebase callable functions response not working

I have a simple firebase functions script setup (running firebase-admin version 8.0 and firebase-functions version 2.3.1): 我有一个简单的firebase函数脚本设置(运行firebase-admin版本8.0和firebase-functions版本2.3.1):

const functions = require('firebase-functions');
const cors = require('cors')({
    origin: true,
});

//Gets and returns a user's ip address
exports.getIPAddress = functions.https.onRequest((req, res) => {
    let ipAddress = req.headers['fastly-client-ip'] || req.connection.remoteAddress;
    ipAddress = ipAddress.toString();
    console.log('Fetched IP Address: ' + ipAddress);
    return cors(req, res, () => {
        res.status(200).send(ipAddress);
    });
});

The function's goal is simply to return to user's IP address. 该功能的目标只是返回用户的IP地址。 It logs fine in the functions console, no errors. 它在函数控制台中记录正常,没有错误。

Here is the client code: 这是客户端代码:

var getIPAddress = mainFirebase.functions().httpsCallable('getIPAddress');
function testIP() {
    getIPAddress().then(function(result) {
        console.log(result.data.text)
    });
}

However, the console says that 'result' is not a valid JSON object. 但是,控制台说'result'不是有效的JSON对象。

I've tried using https.onCall which somebody else on the internet recommended, however, the console says that function doesn't exist. 我尝试过使用互联网上推荐的其他人的https.onCall,但是,控制台说该功能不存在。

Any help getting the response to work properly would be greatly appreciated! 任何帮助让响应正常工作将不胜感激!

Your function is a regular HTTP type function. 您的函数是常规的HTTP类型函数。 However, your client code is attempting to call it as if it were a callable type function. 但是,您的客户端代码试图将其称为可调用类型函数。 That's not going to work. 那不行。 If you want to invoke a callable type function, you'll have to implement the function according to the documentation . 如果要调用可调用类型函数,则必须根据文档实现该函数。

If you need to keep the function as an HTTP type function, you can't use the Firebase client SDK to invoke it. 如果您需要将该功能保留为HTTP类型功能,则无法使用Firebase客户端SDK来调用它。 Just invoke it as if it were any other type of HTTP endpoint . 只需调用它就好像它是任何其他类型的HTTP端点一样

For Callable functions. 对于可调用函数。 You need to create a function like: 你需要创建一个像这样的函数:

exports.addMessage = functions.https.onCall(
  async (data, context) => {
    // context contains the user info.
  }
);

And on your front-end you can call them like: 在你的前端你可以称他们为:

firebase.functions().httpsCallable('addMessage');
addMessage({text: messageText}).then(function(result) {
  // Read result of the Cloud Function.
  var sanitizedMessage = result.data.text;
}).catch(function(error) {
  // Getting the Error details.
  var code = error.code;
  var message = error.message;
  var details = error.details;
  // ...
});

As you are calling an https message. 正在调用https消息。 You can also use the SDK to call https methods. 您还可以使用SDK来调用https方法。 But make sure you are handling CORS on your server. 但请确保您在服务器上处理CORS。

In your client. 在你的客户。 Just use the http client. 只需使用http客户端。

this.http.post method with the function url. this.http.post方法与函数url。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM