【发布时间】:2021-02-19 21:25:40
【问题描述】:
我希望能够在我的 Express.js API 中执行简单的set()。我已经按照文档here 设置了一个配置文件夹,其中包含一个包含以下信息的配置文件:
//config_firebase
var firebase = require("firebase");
const config = {
apiKey: "credentials-go-here",
authDomain: "credentials-go-here",
databaseURL: "credentials-go-here",
projectId: "credentials-go-here",
storageBucket: "credentials-go-here",
messagingSenderId: "credentials-go-here",
appId: "credentials-go-here",
measurementId: "credentials-go-here"
};
module.exports = !firebase.apps.length ? firebase.initializeApp(config) : firebase.app();
在这个文件中,我尝试使用 Firebase 引用写入数据库:
//writeDataService
var app = require("../config/config_firebase");
const writeData = async (uid, userdata) => {
var dbRef2 = app.database()
dbRef2.ref("users/" + uid).child("data").set({
userdata
}, function(error){
if (error){
//if the write failed
return(false)
}
else {
//Data saved successfully
return(true)
}
});
}
module.exports = {
writeData
};
代码永远不会在writeDataService 文件中执行,这意味着即使我导入了我的配置文件,我也无法写入数据库并且我没有得到返回false 或true。如果有的话,我认为我的配置文件有错误。在另一个服务文件中,我通过admin 初始化应用程序,代码如下:
var admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.cert({
projectId: "credentials-go-here",
clientEmail: "credentials-go-here",
privateKey: "credentials-go-here"
}),
databaseURL:"credentials-go-here"
});
任何帮助将不胜感激!我觉得我忽略了某些东西,但我不知道它到底是什么。
【问题讨论】:
标签: node.js firebase rest express firebase-realtime-database