Firebase可调用函数未接收参数

时间:2018-08-15 15:38:05

标签: javascript typescript firebase google-cloud-functions

在打字稿上实现一个简单的函数来调用Firebase可调用函数是行不通的。 使用firebase-tools@4.0.3 -g,firebase-functions @ 2.0.5,firebase @ 5.3.1

Firebase可调用函数:

import * as functions from "firebase-functions";

exports.httpsOnCall = functions.https.onCall((data, context) => {
  console.log(data);
  return data;
});

已正确部署

在webapp中调用该函数:

const a = Firebase.functions.httpsCallable("httpsOnCall");
  a.call({ a: 1, b: "testing", c: true }).then(result => {
    console.log(result);
  });

Firebase对象初始化:

import * as firebase from "firebase";

const config = {
  apiKey: "asdasdasd",
  authDomain: "APPNAME.firebaseapp.com",
  databaseURL: "https://APPNAME.firebaseio.com",
  projectId: "APPNAME",
  storageBucket: "APPNAME.appspot.com",
  messagingSenderId: "123456"
};
firebase.initializeApp(config);
const Firebase = {
   functions: firebase.functions()
}

从Firebase登录:

null

从网络应用登录:

{data: null}

即使填充了上下文变量,数据似乎也没有到达httpsOnCall函数。

2 个答案:

答案 0 :(得分:1)

//imports
const firebase = require("firebase");
require("firebase/functions");


//call steatment
const a = firebase.functions().httpsCallable('httpsOnCall');
        a({ a: 1, b: "testing", c: true })
            .then((result) => {
                console.log(result);
            })
            .catch((error) => {
                console.log(`error: ${JSON.stringify(error)}`);
        });

答案 1 :(得分:0)

代替执行a.call,只需使用参数调用函数a,如下所示:

const a = Firebase.functions.httpsCallable("httpsOnCall");
  a({ a: 1, b: "testing", c: true }).then(result => {
    console.log(result);
  });

有关示例,请参见guide