Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

node.js - Firebase-admin nodejs SDK doens't connect

I'm attempt to connect to firebase/firestone using the nodejs SDK,however I it doesn't connect. I've attempted to connect multiple times, using setInterval but nothing works.

First, I initialize the the firebase using the credentials and the databaseURL, after this I get the databaseRef, and in the end I attempt to write to the database.

I've checked the ./info/connected on setInterval with timeout of 1000ms and mocha --timeout flag to 5000ms, and always marks as offline.

I've checked the credentials, when is a wrong credential or config json, they give an JSON parse error message(cause I have several storage instances, each connected according to a flag spawned during the execution time).

I'm using the TDD approach on my application, so, I have to mock the entire database and check against the resulted values of each operation. I've wrote a controller for the task of handling the firebase/firestone work, but I if I can't connected it has no use.

The code goes here:

const analyticsFirebaseMock = admin.initializeApp({
  credentials: admin.credential.cert(analyticsCredentials),
  databaseURL: process.env.ANALYTICS_FIREBASE_URL
}, 'analyticsMock')

const analyticsDbRef = analyticsFirebaseMock.database()

beforeEach(() => {})

afterEach(() => sinon.restore())

describe('POST - /analytics', () => {
  it('should save the analytics data for new year', async (done) => {
    const itens = 1
    const price = 599.00
    
    setInterval(() => {
      clockAnalyticsDbRef.ref(`.info/connected`).once('value', (value) => {
        if (value.val() === true) console.log('connected')
        else console.error('offline')
      })
    }, 1000)

    await analytics.updateAnalytics(user, itens, price)

    await analyticsDbRef.ref(`${user}`).once('value', (value) => {
      expect(R.view(userLens, value)).to.be.equals(user)
      done()
    })
  })
})

In the above code, I use async/await on analyticsDbRef cause of the asynchronous characteristic of the js. Call the controller, await the query result, conclude with done. The test fails with timeout, expecting done to be called.

What could I doing wrong?

question from:https://stackoverflow.com/questions/65945911/firebase-admin-nodejs-sdk-doenst-connect

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...