我有一个带有节点/表达服务器,React客户端和Firebase Firestore作为数据库的应用程序。当我在firebase数据库上执行操作时,我的代码似乎可以正常工作,但是数据没有显示在firestore控制台中,并且出现以下错误。
我实现了以下解决方案,但是它不起作用: https://stackoverflow.com/a/40321130/1438277
我正在运行npm 6.9.0和节点v11.8.0
谢谢
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 {} } }
这是我从快递服务器发出的数据库调用:
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
}
}
答案 0 :(得分:2)
Firebase(或任何数据库)调用是异步事件,因此您需要使用Promise或回调函数来避免Promise拒绝错误。这是Javascript Promise的文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
这是有关如何将它们与Firebase一起使用的非常有用的文章: https://howtofirebase.com/promises-for-firebase-bbb9d0d595ed
例如,您可以做类似
的操作exports.createAsin = async p => {
try {
let addDoc = await config.db.collection('asins').add({
...
})
console.log('db: ASIN created')
return p
} catch (e) {
...
}
}
答案 1 :(得分:0)
我终于弄明白了:我将Firebase的json认证文件放在.env文件中,这就是造成此错误的原因。我将其移到一个单独的json文件中(并将其添加到.gitignore中),它可以正常工作。 phe!