Upgrading to Cloud Functions v1.0 and I broke ever

2019-07-01 17:35发布

Trying to follow the instructions on this page and I can't figure out how to simply get to the data in the DocumentSnapshot.

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;
});

I get the following error:

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.

1条回答
够拽才男人
2楼-- · 2019-07-01 18:16

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

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.
});

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.

查看更多
登录 后发表回答