我有一个与Firestore接口的GenericDB类,我想扩展该类以使其可用于多个集合:
export default class GenericDB {
constructor(collectionPath) {
this.collectionPath = collectionPath
}
/**
* Create a document in the collection
* @param data
* @param id
*/
async create(data, id = null) {
const collectionRef = (await DB()).collection(this.collectionPath)
const serverTimestamp = firebase.firestore.FieldValue.serverTimestamp()
const dataToCreate = {
...data,
createTimestamp: serverTimestamp,
updateTimestamp: serverTimestamp
}
const createPromise = isNil(id)
? // Create doc with generated id
collectionRef.add(dataToCreate).then(doc => doc.id)
: // Create doc with custom id
collectionRef
.doc(id)
.set(dataToCreate)
.then(() => id)
const docId = await createPromise
return {
id: docId,
...data,
createTimestamp: new Date(),
updateTimestamp: new Date()
}
}
}
但是,当我将GenericDB类扩展到另一个类时,无法调用GenericDB类中的方法。
import GenericDB from './generic-db'
export default class OpenHouseDB extends GenericDB {
constructor(userId) {
super(`open-houses/${userId}/properties`)
}
test() {
// I can call this function successfully
console.log('test worked')
}
}
尝试调用create
类继承的GenericDB类中定义的OpenHouseDB
函数会产生以下错误:
TypeError: Object(...) is not a function
async setup({ commit, rootState }, property) {
try {
const { uid } = rootState.user
const openHouseDb = new OpenHouseDB(uid)
const { mlsId } = property
openHouseDb.test()
const createdOpenHouse = await openHouseDb.create(property, mlsId)
console.log(createdOpenHouse)
} catch (err) {
console.log(err)
}
}
我已经省略了一些代码,但是我已经确认上述代码中的调用openHouseDb.test()
确实可以成功控制台日志正确的字符串。我不确定为什么调用openHouseDb.create(property, mlsId)
会产生TypeError: Object(...) is not a function
。我已经确认openHouseDb
确实有一个create
方法,但我不知道如何调用它。