Firebase托管上的Firebase功能:404

时间:2018-06-02 14:52:53

标签: firebase google-cloud-functions firebase-hosting

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

  

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.如果不透明响应满足您的需求,请将请求的模式设置为“无人”状态'在禁用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"
  }
}

index.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);
  });
});

我在index.html中的脚本

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

我如何初始化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>

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,发现您的问题没有答案,但最终设法弄清楚了。

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

之所以未定义,是因为您的项目ID不属于您最初的Firebase配置对象。像我一样,您可能已经从Firebase复制并粘贴了JS SDK的启动程序片段,但是您在它们开始包含Project ID作为其一部分之前就已经做到了。出于某种原因,即使现在需要项目ID来构建云功能URL,但如果您不包含SDK,SDK也不会出错/警告。

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

projectId: <YOUR_PROJECT_ID_HERE>

这时您应该不再看到404请求。