sampl / firefly

Web app boilerplate for beginners based on Firebase and React 🔥
http://getfirefly.org/
234 stars 42 forks source link

TypeError: Cannot read property 'secret_key' of undefined #160

Closed SamBroner closed 5 years ago

SamBroner commented 5 years ago

Error while running npm run start or npm run serve in /functions

Seems like const STRIPE_SECRET_KEY = functions.config().stripe.secret_key shows config as null

SamBroner commented 5 years ago

This issue is caused when running functions locally. functions.config() is not available in the local runtime, only in the GCF runtime. (I believe)

sampl commented 5 years ago

@SamBroner hm, that should work... have you set and deployed your config to the "dev" project? That's what npm start uses.

Try adding your keys to this file (also pasted below) and pasting the whole thing into your terminal--that will set the project to dev, update your keys, and deploy. Does it work after that?

firebase use dev && \
  firebase functions:config:set \
    stripe.secret_key="sk_12345678901234567890" \
    stripe.plan_id="plan_1234567890" \
    algolia.app_id="1234567890" \
    algolia.admin_key="123456789012345678901234567890" \
  && firebase deploy --only functions
sampl commented 5 years ago

Closing for now, but let's re-open if that didn't solve it

MarkEdmondson1234 commented 4 years ago

I had the same issue when I tried to deploy via npm run dev even though the env.dev file was updated with my keys but running it in the console did work:

Failed:

> npm run dev

> xxxx@0.1.0 dev /Users/xxxx/dev/npm/xxxxxxxx
>   sh -ac '. ./.env.dev;   firebase use dev   && react-scripts build && firebase deploy'

Now using alias dev (xxxxxx)
Creating an optimized production build...
Compiled successfully.

File sizes after gzip:

  215.19 KB  build/static/js/2.e4527099.chunk.js
  4.32 KB    build/static/js/main.d07e3105.chunk.js
  776 B      build/static/js/runtime-main.cbcc5bbb.js

The project was built assuming it is hosted at /.
You can control this with the homepage field in your package.json.

The build folder is ready to be deployed.
You may serve it with a static server:

  npm install -g serve
  serve -s build

Find out more about deployment here:

  bit.ly/CRA-deploy

=== Deploying to 'xxxxxx'...

i  deploying firestore, functions, hosting
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint /Users/xxxxxx/dev/npm/xxxxxx/functions
> eslint .

✔  functions: Finished running predeploy script.
i  cloud.firestore: checking firestore.rules for compilation errors...
✔  cloud.firestore: rules file firestore.rules compiled successfully
i  functions: ensuring necessary APIs are enabled...
✔  functions: all necessary APIs are enabled
i  firestore: uploading rules firestore.rules...
i  functions: preparing functions directory for uploading...

Error: Error occurred while parsing your function triggers.

TypeError: Cannot read property 'secret_key' of undefined
    at Object.<anonymous> (/Users/xxxxx/dev/npm/xxxxx/functions/lib/subscriptions.js:6:52)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (/Users/xxxxx/dev/npm/xxxxx/functions/index.js:7:23)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! xxxx@0.1.0 dev: `  sh -ac '. ./.env.dev;   firebase use dev   && react-scripts build && firebase deploy'`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the xxxx@0.1.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/xxxx/.npm/_logs/2020-03-13T16_53_01_515Z-debug.log

Pasting in the contents as suggested above did work though:

> firebase use dev && \
>   firebase functions:config:set \
>     stripe.secret_key="sk_test_xxxxx" \
>     stripe.plan_id="prod_xxxxxx" \
>     algolia.app_id="1234567890" \
>     algolia.admin_key="123456789012345678901234567890" \
>   && firebase deploy --only functions
Now using alias dev (xxxxx)
✔  Functions config updated.

Please deploy your functions for the change to take effect by running firebase deploy --only functions

=== Deploying to 'xxxxx'...

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint /Users/xxxxx/dev/npm/xxxxx/functions
> eslint .

✔  functions: Finished running predeploy script.
i  functions: ensuring necessary APIs are enabled...
✔  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (42.1 KB) for uploading
✔  functions: functions folder uploaded successfully
i  functions: creating Node.js 10 (Beta) function updateStripeSubscription(us-central1)...
i  functions: creating Node.js 10 (Beta) function updatePostLikeCount(us-central1)...
✔  functions[updatePostLikeCount(us-central1)]: Successful create operation. 
✔  functions[updateStripeSubscription(us-central1)]: Successful create operation. 

✔  Deploy complete!

Project Console: https://console.firebase.google.com/project/xxxxx/overview
MarkEdmondson1234 commented 4 years ago

Um it was because it should have been firebase deploy :)