1

I have an app with a node/express server, a React client, and firebase firestore as database. When I do an operation on the firebase database, my code seems to work, but the data does not show up in the firestore console and I get the error below.

I implemented the solution below but it does not work: https://stackoverflow.com/a/40321130/1438277

I am running npm 6.9.0 and node v11.8.0

Thank you

Unhandled Rejection at: Promise Promise {
[0]   <rejected> Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
[0]       at Sign.sign (internal/crypto/sig.js:84:29)
[0]       at Object.sign (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/jwa/index.js:152:45)
[0]       at Object.jwsSign [as sign] (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/jws/lib/sign-stream.js:32:24)
[0]       at GoogleToken.requestToken (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/gtoken/build/src/index.js:180:31)
[0]       at GoogleToken.getTokenAsync (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/gtoken/build/src/index.js:119:21)
[0]       at GoogleToken.getToken (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/gtoken/build/src/index.js:61:21)
[0]       at JWT.refreshTokenNoCache (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/google-auth-library/build/src/auth/jwtclient.js:129:36)
[0]       at JWT.refreshToken (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/google-auth-library/build/src/auth/oauth2client.js:148:24)
[0]       at JWT.getRequestMetadataAsync (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/google-auth-library/build/src/auth/oauth2client.js:265:28)
[0]       at JWT.getRequestMetadataAsync (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/google-auth-library/build/src/auth/jwtclient.js:76:26)
[0]       at JWT.getRequestHeaders (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/google-auth-library/build/src/auth/oauth2client.js:244:32)
[0]       at SingleCallCredentials.call_credentials_1.CallCredentials.createFromMetadataGenerator [as metadataGenerator] (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/@grpc/grpc-js/build/src/index.js:74:39)
[0]       at Promise (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/@grpc/grpc-js/build/src/call-credentials.js:64:18)
[0]       at new Promise (<anonymous>)
[0]       at SingleCallCredentials.generateMetadata (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/@grpc/grpc-js/build/src/call-credentials.js:63:16)
[0]       at Promise.all.creds.map (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/@grpc/grpc-js/build/src/call-credentials.js:47:75) } reason: Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
[0]     at Sign.sign (internal/crypto/sig.js:84:29)
[0]     at Object.sign (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/jwa/index.js:152:45)
[0]     at Object.jwsSign [as sign] (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/jws/lib/sign-stream.js:32:24)
[0]     at GoogleToken.requestToken (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/gtoken/build/src/index.js:180:31)
[0]     at GoogleToken.getTokenAsync (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/gtoken/build/src/index.js:119:21)
[0]     at GoogleToken.getToken (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/gtoken/build/src/index.js:61:21)
[0]     at JWT.refreshTokenNoCache (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/google-auth-library/build/src/auth/jwtclient.js:129:36)
[0]     at JWT.refreshToken (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/google-auth-library/build/src/auth/oauth2client.js:148:24)
[0]     at JWT.getRequestMetadataAsync (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/google-auth-library/build/src/auth/oauth2client.js:265:28)
[0]     at JWT.getRequestMetadataAsync (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/google-auth-library/build/src/auth/jwtclient.js:76:26)
[0]     at JWT.getRequestHeaders (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/google-auth-library/build/src/auth/oauth2client.js:244:32)
[0]     at SingleCallCredentials.call_credentials_1.CallCredentials.createFromMetadataGenerator [as metadataGenerator] (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/@grpc/grpc-js/build/src/index.js:74:39)
[0]     at Promise (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/@grpc/grpc-js/build/src/call-credentials.js:64:18)
[0]     at new Promise (<anonymous>)
[0]     at SingleCallCredentials.generateMetadata (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/@grpc/grpc-js/build/src/call-credentials.js:63:16)
[0]     at Promise.all.creds.map (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/@grpc/grpc-js/build/src/call-credentials.js:47:75)
[0] (node:3612) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 2)
[0] Unhandled Rejection at: Promise Promise {
[0]   <rejected> { Error: Getting metadata from plugin failed with error: error:0906D06C:PEM routines:PEM_read_bio:no start line
[0]       at Http2CallStream.call.on (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/@grpc/grpc-js/build/src/client.js:102:45)
[0]       at Http2CallStream.emit (events.js:202:15)
[0]       at Http2CallStream.EventEmitter.emit (domain.js:439:20)
[0]       at Http2CallStream.endCall (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/@grpc/grpc-js/build/src/call-stream.js:74:18)
[0]       at /Users/antoine/Desktop/jf_express_with_react/server/node_modules/@grpc/grpc-js/build/src/call-stream.js:355:18
[0]       at processTicksAndRejections (internal/process/next_tick.js:81:5)
[0]     code: 2,
[0]     details:
[0]      'Getting metadata from plugin failed with error: error:0906D06C:PEM routines:PEM_read_bio:no start line',
[0]     metadata: Metadata { internalRepr: Map {} } } } reason: { Error: Getting metadata from plugin failed with error: error:0906D06C:PEM routines:PEM_read_bio:no start line
[0]     at Http2CallStream.call.on (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/@grpc/grpc-js/build/src/client.js:102:45)
[0]     at Http2CallStream.emit (events.js:202:15)
[0]     at Http2CallStream.EventEmitter.emit (domain.js:439:20)
[0]     at Http2CallStream.endCall (/Users/antoine/Desktop/jf_express_with_react/server/node_modules/@grpc/grpc-js/build/src/call-stream.js:74:18)
[0]     at /Users/antoine/Desktop/jf_express_with_react/server/node_modules/@grpc/grpc-js/build/src/call-stream.js:355:18
[0]     at processTicksAndRejections (internal/process/next_tick.js:81:5)
[0]   code: 2,
[0]   details:
[0]    'Getting metadata from plugin failed with error: error:0906D06C:PEM routines:PEM_read_bio:no start line',
[0]   metadata: Metadata { internalRepr: Map {} } }

This is the database call I am making from my express server:

const admin = require('firebase-admin')
const config = require('./config.js')

exports.createAsin = p => {
  try {
    let addDoc = config.db.collection('asins').add({
      uid: p.uid,
      asin: p.asin,
      mapPrice: p.mapPrice,
      data: []
    })
    console.log('db: ASIN created')
    return p
  } catch (e) {
    console.log('db: error creating ASIN /', e)
    return null
  }
}
blrsk
  • 185
  • 1
  • 4
  • 11
  • I think this promise rejection issue, Nedd to find in your code like where is your promise broken. you can find or debug it by event `process.on('unhandledRejection', (reason, p) => { console.log('Unhandled Rejection at: Promise', p, 'reason:', reason); })`; or you can take description from [Here](https://thecodebarbarian.com/unhandled-promise-rejections-in-node.js.html) – Paresh Barad May 30 '19 at 13:24
  • I've updated the question to include the error I now get. Thanks! – blrsk May 30 '19 at 13:27
  • Any idea what might be causing the error? Thanks – blrsk May 30 '19 at 19:59

2 Answers2

2

Firebase (or any database) calls are asynchronous events, so you need to use promises or callback functions to avoid promise rejection errors. Here is the docs for Javascript promises: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

And here is a pretty helpful article on how to use them with Firebase: https://howtofirebase.com/promises-for-firebase-bbb9d0d595ed

For your example, you could do something like

exports.createAsin = async p => {
  try {
    let addDoc = await config.db.collection('asins').add({
      ...
    })
    console.log('db: ASIN created')
    return p
  } catch (e) {
    ...
  }
}
natn2323
  • 1,983
  • 1
  • 13
  • 30
jmink
  • 21
  • 1
  • Thank you for your contribution :) I rewrote the function following your advice but it still get the same error. I've never seen an error like this related to Firebase so I don't think that's the issue, but maybe. – blrsk May 31 '19 at 17:14
0

I finally figured it out: my json certification file for Firebase was placed in a .env file and that’s what created the error. I moved it out into a separate json file (and added it to .gitignore) and it works. phew!

blrsk
  • 185
  • 1
  • 4
  • 11