这是我index.js
文件的一部分:
const firebase = require("firebase");
const functions = require('firebase-functions');
// Firebase Setup
const admin = require('firebase-admin');
const serviceAccount = require('./service-account.json');
const cors = require('cors')({ origin: true });
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
exports.addMessage = functions.https.onCall((data, context) => {
const sanitizedMessage = sanitizer.sanitizeText("test");
return admin.database().ref('/messages').push({
text: sanitizedMessage
}).then(() => {
console.log('New Message written');
return { text: sanitizedMessage };
})
});
此功能已成功上传到Firebase。我试图使用此代码从我的Web客户端调用它:
var addMessage = firebase.functions().httpsCallable('addMessage');
addMessage({text: "test"}).then(function(result) {
var sanitizedMessage = result.data.text;
console.log(sanitizedMessage);
});
结果出现以下描述错误:
此外,Firebase控制台中没有日志。如果我直接访问该链接,我会收到一条日志消息。这意味着两件事:
我完全陷入困境,因为我跟随official guide。
非常感谢任何有关调试的帮助。
答案 0 :(得分:0)
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const cors = require('cors')({ origin: true });
admin.initializeApp();
exports.addMessage = functions.https.onCall((data, context) => {
const sanitizedMessage = sanitizer.sanitizeText("test");
return admin.database().ref('/messages').push({
text: sanitizedMessage
}).then(() => {
console.log('New Message written');
return { text: sanitizedMessage };
})
});
答案 1 :(得分:0)
我已经彻底地尝试并测试了您的代码,可能存在两个问题。
我假设您打算使用此npm清理程序包:https://www.npmjs.com/package/sanitizer,因此这些问题可能是:
sanitizeText
不是sanitazer模块的方法,而sanitize
是以下代码效果很好:
const functions = require('firebase-functions');
const sanitizer = require('sanitizer');
const admin = require('firebase-admin');
const cors = require('cors')({ origin: true });
admin.initializeApp();
exports.addMessage = functions.https.onCall((data, context) => {
const sanitizedMessage = sanitizer.sanitize("test");
console.log(sanitizedMessage);
return admin.database().ref('/messages').push({
text: sanitizedMessage
}).then(() => {
console.log('New Message written');
return { text: sanitizedMessage };
})
});