6

I want to integrate the new Cloud Firestore to my Cloud Functions.

I updated node.js and installed the latest firebase version on my mac.

The Documentation says:

exports.myFunctionName = functions.firestore
  .document('users/marie').onWrite((event) => {
    // ... Your code here
 });

should work. I just copied the code and pasted it into the index.js, like every other realtime database functions. When I deploy the function code ($firebase deploy --only functions), i get this error:

Cannot read property 'document' of undefined
    at Object.<anonymous> (/private/var/folders/zm/wp2415l929s472jv7kzbt3km0000gn/T/fbfn_21520Y4xqAJosBNHe/index.js:196:45)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)

Any suggestions/ideas on the problem?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
bobski
  • 152
  • 3
  • 14
  • 1
    What version of `firebase-functions` do you have in your project? You should upgrade it to the latest, since Cloud Firestore was just released. – Michael Bleigh Oct 11 '17 at 19:59
  • 1
    You probably have an old version of the SDK. Update it as [described in the documentation](https://firebase.google.com/docs/functions/get-started#set_up_and_initialize_functions_sdk) by running `npm install firebase-functions@latest --save` in your project's functions directory – Bob Snyder Oct 11 '17 at 20:02
  • Already did that, but I tried again. Restarted my mac, but still the same error – bobski Oct 12 '17 at 08:13
  • 1
    I had to change my package.json to make it work. – J. Doe Oct 12 '17 at 09:59
  • @J.Doe. what did you change exactly? – bobski Oct 12 '17 at 10:51

5 Answers5

2

Try changing your package to:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "dependencies": {
    "firebase-admin": "~5.4.0",
    "firebase-functions": "^0.7.0"
  },
  "private": true
}

And run a update after that in the terminal (npm update).

J. Doe
  • 12,159
  • 9
  • 60
  • 114
2

Just calling firebase init did it for me. (this also changed the Package.json file)

Gertjan.com
  • 410
  • 1
  • 3
  • 12
1

I went through the process of creating a brand new project in a new folder and it got rid of the error for me.

the_kaseys
  • 274
  • 1
  • 2
  • 17
0

Alright, i tried all these things, but it didn't work out. But firebase brought out an update, now it works :)

Seems like they had some bugs with it.

Thanks for your help!

bobski
  • 152
  • 3
  • 14
0

As mentioned in firebase-documentation.

In many cases, new features and bug fixes are available only with the latest version of the Firebase CLI and firebase-functions SDK. It's a good practice to frequently update both the Firebase CLI and the SDK with these commands inside the functions folder of your Firebase project:

Always ensure you are using updated packages.

npm install firebase-functions@latest --save
npm install -g firebase-tools
Ahsaan Yousuf
  • 685
  • 7
  • 16