错误消息:
TypeError: _this.db is not a function
代码:
import app from "firebase/app";
import "firebase/auth";
import "firebase/database";
const config = {
apiKey: "some-api-key",
authDomain: "myapp.firebaseapp.com",
databaseURL: "https://myapp.firebaseio.com",
projectId: "aofndiuf",
storageBucket: "project-somenumber.appspot.com",
messagingSenderId: "793813245724y6597"
};
class Firebase {
constructor() {
app.initializeApp(config);
this.auth = app.auth();
this.db = app.database();
}
// Auth API
doCreateNewsletter = (news, name, description, email) => {
const newsletter = { news, name, description, email };
const newPostKey = this.db()
.ref()
.child("newsletters")
.push().key;
return this.db
.ref()
.child("/newsletters/" + newPostKey)
.set(newsletter);
};
最佳答案
您像这样分配了 this.db
:
this.db = app.database();
但是稍后你会这样引用它:
const newPostKey = this.db()
...
从app.database()
返回的对象不是一个函数,所以你不能像现在这样像函数一样调用它。这是Database目的。也许您可以去掉这些括号,使您的代码按照您预期的方式工作。
关于javascript - firebase.database() 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54992800/