Firebase 托管上的 Firebase 功能:404

mut*_*n96 7 firebase firebase-hosting google-cloud-functions

我试图从应用程序调用函数,但它不起作用,并且我从控制台收到以下错误:

index.esm.js:402 选项https://us-central1-undefined.cloudfunctions.net/addMessage 404 () 无法加载https://us-central1-undefined.cloudfunctions.net/addMessage:对预检请求的响应不未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问来源“ https://MYWEBADDRESS ”。响应的 HTTP 状态代码为 404。如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以在禁用 CORS 的情况下获取资源。

firebase.json:

{
  "database": {
    "rules": "database.rules.json"
  },
  "hosting": {
    "public": "public",
    "rewrites": [
      {
        "source": "**",
        "function": "addMessage"
      }
    ]
  },
  "functions": {
    "predeploy": [
      "npm --prefix $RESOURCE_DIR run lint"
    ],
    "source": "functions"
  }
}
Run Code Online (Sandbox Code Playgroud)

索引.js

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

const admin = require('firebase-admin');
admin.initializeApp();


exports.addMessage = functions.https.onCall((data, context) => {

    // Message text passed from the client.
    const text = data.text;

        // Checking attribute.
    if (!(typeof text === 'string') || text.length === 0) {
      // Throwing an HttpsError so that the client gets the error details.
      throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
          'one arguments "text" containing the message text to add.');
    }
    // Checking that the user is authenticated.
    if (!context.auth) {
      // Throwing an HttpsError so that the client gets the error details.
      throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
          'while authenticated.');
    }

    // Saving the new message to the Realtime Database.
    return admin.database().ref('/messages').push({
      text: text
    }).then(() => {
      console.log('New Message written');
      // Returning the sanitized message to the client.
      return { text: sanitizedMessage };
    }).catch((error) => {
    // Re-throwing the error as an HttpsError so that the client gets the error details.
    throw new functions.https.HttpsError('unknown', error.message, error);
  });
});
Run Code Online (Sandbox Code Playgroud)

我的脚本在index.html中

var addMessage = firebase.functions().httpsCallable('addMessage');
addMessage({text: "messageText"}).then(function(result) {
     var message = result.data.text;
     console.log(message);
});
Run Code Online (Sandbox Code Playgroud)

我如何初始化 Firebase:

  <script src="https://www.gstatic.com/firebasejs/5.0.4/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/5.0.4/firebase-auth.js"></script>
  <script src="https://www.gstatic.com/firebasejs/5.0.4/firebase-database.js"></script>
  <script src="https://www.gstatic.com/firebasejs/5.0.4/firebase-functions.js"></script>
    <script>
      // Initialize Firebase
      var config = {
        apiKey: "**",
        authDomain: "***",
        databaseURL: "***",
        storageBucket: "***",
      };
      firebase.initializeApp(config);
      var functions = firebase.functions();
    </script>
Run Code Online (Sandbox Code Playgroud)

cas*_*ber 6

我遇到了同样的问题,发现你的问题没有答案,但最终设法解决了。

正如@Doug Stevenson 在上面的评论中提到的,问题是您看到的 Cloud Functions URL 而不是undefined您的项目 ID 作为 url 子域的最后一部分。

它未定义的原因是您的项目 ID 不是初始 Firebase 配置对象的一部分。和我一样,您可能从 Firebase 中复制并粘贴了 JS SDK 的起始代码片段,但您是在他们开始将项目 ID 包含在其中之前完成的。由于某种原因,即使现在需要项目 ID 来构建云函数 URL,但如果您不包含它,SDK 也不会发出错误/警告。

您需要做的就是将以下字段添加到您的config对象中:

projectId: <YOUR_PROJECT_ID_HERE>
Run Code Online (Sandbox Code Playgroud)

然后您应该会看到请求不再是 404。