【发布时间】:2021-10-10 02:17:09
【问题描述】:
我正在使用 Firebase Firestore 并使用 Firebase 提供的自定义对象代码开展项目。我在一个可能触发的函数中使用了一个转换器,并收到一个错误,指出 fromFirestore 不是一个函数。我不明白为什么。我添加了 City 类文件、云触发函数以及在日志中发现的错误消息。任何帮助将不胜感激。
城市类
class City {
constructor(name, state, country) {
this.name = name;
this.state = state;
this.country = country;
}
toString() {
return this.name + ', ' + this.state + ', ' + this.country;
}
}
// Firestore data converter
var cityConverter = {
toFirestore: function (city) {
return {
name: city.name,
state: city.state,
country: city.country,
};
},
fromFirestore: function (snapshot, options) {
const data = snapshot.data(options);
return new City(data.name, data.state, data.country);
},
};
// Exports
module.exports = {
City: City,
};
module.exports.cityConverter = cityConverter;
云触发功能
exports.cityUpdate = functions.firestore
.document('cities/{cityId}')
.onUpdate(async (change, context) => {
const data = change.after.data();
const doc = change.after;
const cityDoc = await db
.collection('cities')
.doc('LV')
.withConverter(cityConverter)
.get();
if (cityDoc.exists) {
const city = cityDoc.data();
console.log(city);
console.log(city.name);
} else {
console.log('No listing exists');
}
});
错误日志
TypeError: this.ref._converter.fromFirestore 不是函数 在 QueryDocumentSnapshot.data (/workspace/node_modules/@google-cloud/firestore/build/src/document.js:311:40) 在 QueryDocumentSnapshot.data (/workspace/node_modules/@google-cloud/firestore/build/src/document.js:488:28) 在exports.cityUpdate.functions.firestore.document.onUpdate (/workspace/functions/cities.js:28:28) 在 process._tickCallback (internal/process/next_tick.js:68:7)
【问题讨论】:
标签: firebase google-cloud-firestore