Angular Firebase云函数显示错误“;否';访问控制允许原点';请求的资源上存在标头。”;

Angular Firebase云函数显示错误“;否';访问控制允许原点';请求的资源上存在标头。”;,angular,firebase,google-cloud-firestore,Angular,Firebase,Google Cloud Firestore,Firebase平台调用Firebase cloud函数时,会出现以下错误: 职位 500()跳闸:1未能加载 : 请求的服务器上不存在“Access Control Allow Origin”标头 资源。来源“ 因此不允许访问。响应具有HTTP状态代码 500跨源读取阻塞(CORB)阻塞的跨源响应 使用MIME类型text/plain。看见 更多 细节 在云函数代码中,我尝试使用不同的Firebase数据库来存储消息:Firestore和realtime数据库。我用实时数据库评论了这个部分。非

Firebase平台调用Firebase cloud函数时,会出现以下错误:

职位 500()跳闸:1未能加载 : 请求的服务器上不存在“Access Control Allow Origin”标头 资源。来源“ 因此不允许访问。响应具有HTTP状态代码 500跨源读取阻塞(CORB)阻塞的跨源响应 使用MIME类型text/plain。看见 更多 细节

在云函数代码中,我尝试使用不同的Firebase数据库来存储消息:Firestore和realtime数据库。我用实时数据库评论了这个部分。非常奇怪的是,这个错误只有在我使用Firestore时才会发生。其他部分是相同的,只有当我使用Firestore时,错误才会发生

在Firebase platform side()中,函数向Firebase cloud函数发布位置消息,代码如下:

postGeolocation(){
      //AJAX POST JSON from javascript to nodejs server
      var xhr = new XMLHttpRequest();
      var timestamp = Math.round((new Date()).getTime() / 1000);
      // HTTP Option1: Firebase cloud functions: addMessage (secure path)
      var url = "https://us-central1-xxxxxxx-android-app.cloudfunctions.net/addMessage";

      console.log("latitude: "+this.globalvar.latitude + " " + "longitude: "+this.globalvar.longitude);
      xhr.open("POST", url, true);
      xhr.setRequestHeader("Content-Type", "application/json");
      xhr.onreadystatechange = function () {
          if (xhr.readyState === 4 && xhr.status === 200) {
              var json = JSON.parse(xhr.responseText);
            //  console.log(json.email + ", " + json.password);
          }
      };

      var data = JSON.stringify({"tag": this.globalvar.userIdentifier, "latitude":this.globalvar.latitude, "longitude":this.globalvar.longitude, "timestamp": timestamp});
      xhr.send(data);
  }
在cloud function side()中,完整的代码是:

'use strict';

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const cors = require('cors')({origin: true});

// The Firebase Admin SDK to access the Firebase Firestore.
admin.initializeApp(functions.config().firebase);
var db = admin.firestore();

// The Firebase Admin SDK to access the Firebase Realtime Database.
//admin.initializeApp();

// Take the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /messages/:pushId/original
exports.addMessage = functions.https.onRequest((req, res) => {
  cors(req, res, () => {
    // Grab the text parameter.
    const user = req.body.tag;
    const latitude = req.body.latitude;
    const longitude = req.body.longitude;
    const timestamp = req.body.timestamp;

    var date = new Date();
    var n = date.toDateString();
    var time = date.toLocaleTimeString();
    var datetime = n + ' ' + time;
    console.log("send data to realtime db");

    // Push the new message into the Realtime Database using the Firebase Admin SDK.
    // var msgRef = admin.database().ref('/messages').push({user: user, latitude: latitude, longitude: longitude, timestamp: timestamp, servertime: datetime});
    // res.status(200).send(msgRef);

    // Add the new message into the Firestore
    var docRef = db.collection('messages').doc(timestamp);
    var setAda = docRef.set({
      user: user,
      latitude: latitude,
      longitude: longitude,
      timestamp: timestamp,
      servertime: datetime
    });
    res.status(200).send(setAda);
  });
});
你能帮我解决这个问题吗?
非常感谢

在Firebase控制台上检查函数日志后,我发现我在函数中放置了错误的文档路径

var docRef = db.collection('messages').doc(timestamp);
这里时间戳是数字类型,但函数在这里请求字符串类型。在我将类型更改为string之后,问题就解决了


谢谢

你应该把你的承诺串起来,如下:
var setAda=docRef.set({…})。然后(()=>{res.status(200)。send(setAda);})。catch(err=>{res.status(500)。send(err);})
。我建议您观看Firebase团队关于云功能和承诺的两个“必看”视频:和。