在React应用程序中使用Firestore,我使用Google Cloud Functions测试以获取文档字段,然后使用该字段在Firestore中创建新字段。我有一天工作,现在不是。
这是我在Google云端功能日志中出现的错误:
错误:无法解析来自Firestore值的类型:DocumentSnapshot._decodeValue上的{“stringValue”:“sox”}
以下是我使用的代码。它与这里的代码非常相似:https://firebase.google.com/docs/firestore/extend-with-functions#writing_data
exports.createShowDoubleName = functions.firestore
.document('shows/{showId}')
.onUpdate((event) => {
const data = event.data.data();
const previousData = event.data.previous.data();
if (data.name == previousData.name) return;
var name = data.name;
var newname = name+"_"+name
return event.data.ref.set({
newname: newname
}, {merge: true});
});
firebase-functions SDK version 0.7.5应该可以解决这个问题。它现在强制对firebase-admin 5.5.0的对等依赖。得到它:
npm install [email protected]
运行firebase-functions
的最新版本(0.7.4)时,我无法重现错误。
您必须运行旧版本,因为当前版本的firebase-functions
需要触发器函数来返回Promise或值。对于当前版本,此语句会产生错误:
if (data.name == previousData.name) return;
并且必须更改为:
if (data.name == previousData.name) return null;
建议您通过在项目的firebase-functions
文件夹中运行此命令来升级functions
:
npm install firebase-functions@latest --save
我有同样的问题和相同的错误。在升级了firebase-functions和firebase-admin(npm install firebase-functions @ latest --save)和(npm install firebase-admin @ latest --save)之后,它解决了这个问题。进一步研究日志问题似乎是旧版本的firebase-admin。我现在运行管理员版本5.5.1和功能版本0.7.4。
绝对只需要更新一些东西。
打开终端导航到functions文件夹并从那里运行此更新:
npm install firebase-functions@latest firebase-admin@latest --save