Reputation: 1829
When the Firebase platform calls the Firebase cloud function, it gives below error:
POST https://us-central1-xxxxxxx-device-android-app.cloudfunctions.net/addMessage 500 () trip:1 Failed to load https://us-central1-xxxxxxx-device-android-app.cloudfunctions.net/addMessage: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://xxxxxxx-device-android-app.firebaseapp.com' is therefore not allowed access. The response had HTTP status code 500. Cross-Origin Read Blocking (CORB) blocked cross-origin response https://us-central1-xxxxxxx-device-android-app.cloudfunctions.net/addMessage with MIME type text/plain. See https://www.chromestatus.com/feature/5629709824032768 for more details.
In the cloud function code, I have tried using different Firebase database to store the messages: Firestore and realtime database. I have commented the part using realtime database. The very strange thing is this error only happens when I use Firestore. Other part are the same, only when I use Firestore, the error happens...
In Firebase platform side(https://xxxxxxx-device-android-app.firebaseapp.com), function posts a location message to the Firebase cloud function, the code is as:
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);
}
In cloud function side( https://us-central1-xxxxxxx-device-android-app.cloudfunctions.net/addMessage ), the full code is:
'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);
});
});
Could you please help me to solve this problem? Many thanks
Upvotes: 1
Views: 1960
Reputation: 1829
After checking function log on Firebase console, I found I put a wrong document path in the function.
var docRef = db.collection('messages').doc(timestamp);
here timestamp is number type, but the function requests a string type here. After I change the type to string, the issue is solved.
Thanks
Upvotes: 1