升级到 Cloud Functions v1.0,我破坏了一切

nin*_*00b 5 google-cloud-functions google-cloud-firestore

尝试按照本页上的说明进行操作,但我不知道如何简单地获取 DocumentSnapshot 中的数据。

他们并没有真正解释更新 onCreate,只是说它与 onDelete 相同,但我无法让它工作。

 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;
});
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

类型错误:snap.data 不是exports.mFoo.functions.firestore.document.onCreate 中的函数

我确信这非常简单,但我不太理解这些东西,而且我尝试了很多东西的组合,但没有任何效果。

nin*_*00b 5

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
Run Code Online (Sandbox Code Playgroud)

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"
  },
Run Code Online (Sandbox Code Playgroud)

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"
  },
Run Code Online (Sandbox Code Playgroud)

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.
});
Run Code Online (Sandbox Code Playgroud)

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.