升级到Cloud Functions v1.0后,我一切都中断了 [英] Upgrading to Cloud Functions v1.0 and I broke everything
问题描述
尝试遵循此页面上的说明并且我不知道如何简单地获取DocumentSnapshot中的数据.
Trying to follow the instructions on this page and I can't figure out how to simply get to the data in the DocumentSnapshot.
除了说它与onDelete相同之外,他们并没有真正解释更新onCreate的事情,而我无法使之工作.
They do not really explain updating onCreate, except to say it is the same as onDelete, and I can't get that to work.
exports.mFoo = functions.firestore
.document('foos/{key}')
.onCreate((snap, context) => {
const bar = snap.data(); // <-- DOESN'T WORK
console.log(bar); // <-- DOESN'T WORK
return Promise;
});
我收到以下错误:
TypeError:snap.data不是函数 Exports.mFoo.functions.firestore.document.onCreate
TypeError: snap.data is not a function at exports.mFoo.functions.firestore.document.onCreate
我确信它非常简单,但是我不太了解这些东西,我尝试了无数种东西的组合,但是没有用.
I'm sure it's super simple but I don't really understand this stuff and I have tried tons of combinations of stuff and nothing works.
推荐答案
Turns out I ran into the same problem that I have before.
在执行以下任何类型的更新之前:
Before doing any of these types of updates:
npm install firebase-functions@latest --save
npm install firebase-admin@5.11.0 --save
npm install -g firebase-tools
首先,我必须打开我的package.json文件并删除所有将要更新的依赖项.
First I have to open my package.json file and delete any dependencies that are going to be updated.
这些就是我在那找到的东西
These were what I found there:
"dependencies": {
"firebase-admin": "~5.8.1",
"firebase-functions": "^0.8.1"
},
将相关性保留为空,然后重新运行这些命令进行安装后,将显示以下内容:
After leaving the dependencies empty and rerunning those commands to install, these showed up:
"dependencies": {
"firebase-admin": "^5.11.0",
"firebase-functions": "^1.0.1"
},
显然,安装和升级不会解决这些依赖关系,但是如果不存在,它们会添加它们.也许可以通过在其中键入来修复它,但我不知道要在其中放置什么版本号.
Apparently installs and upgrades won't fix these dependencies but they will add them if they aren't there. Maybe it is possible to fix it by typing those in but I wouldn't have had a clue what version numbers to put there.
现在,(修改后的)代码有效:
Now the (modified) code works:
exports.mFoo = functions.firestore
.document('foos/{key}')
.onCreate((snap, context) => {
const bar = snap.data(); // now this works
console.log(bar.baz); // <-- before I wasn't referring to anything in the snapshot
return Promise; // I. Promised. Nothing.
});
感谢 @Todd Kerpelman 为我指出了正确的方向,并@Bob Snyder ,因为
Thanks @Todd Kerpelman for pointing me in the right direction and @Bob Snyder because the same answer from the other post worked for me here also.
这篇关于升级到Cloud Functions v1.0后,我一切都中断了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!