调用Firebase Cloud Functions

时间:2018-11-03 00:40:36

标签: javascript firebase http google-cloud-functions

我具有以下Hello World Firebase云功能:

const functions = require('firebase-functions');

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {
    response.send("Hello from Firebase!");
});

然后我从网页中调用它,就像这样:

<script>
    // Initialize Firebase
    var config = { <config information copy-pasted from firebase console>
    };
    firebase.initializeApp(config);

    let helloWorld = firebase.functions().httpsCallable('helloWorld');
    console.log(helloWorld)
    helloWorld().then(function (result) {

    })
</script>

但是,出现以下错误:

  

通过CORS策略阻止从来源“空”获取(我的云功能网址)访问:对预检请求的响应未通过访问控制检查:不存在“ Access-Control-Allow-Origin”标头在请求的资源上。如果不透明的响应满足您的需求,请将请求的模式设置为“ no-cors”以在禁用CORS的情况下获取资源。

我仍然可以直接访问HTTP端点。我在做什么错了?

查看以下内容后,我尝试了以下操作:Enabling CORS in Cloud Functions for Firebase

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

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {

    return cors(req, res, () => {
        res.status(200).send('Hello World!')
    })
    //response.set('Access-Control-Allow-Origin', '*');
    //response.send("Hello from Firebase!");
});

但是,当我访问HTTP端点时,出现错误:

  

错误:无法处理请求

0 个答案:

没有答案