flamelink / flamelink-js-sdk

🦊 Official Flamelink JavaScript SDK for both the Firebase Realtime database and Cloud Firestore
https://flamelink.github.io/flamelink-js-sdk
MIT License
43 stars 5 forks source link

Not working server-side with Firebase admin & functions #136

Closed ribalnasr closed 4 years ago

ribalnasr commented 4 years ago

I've been trying to get flamelink to work in a firebase function with no success. the result has no error but returns undefined, here's my code:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
const firebaseApp = admin.initializeApp();

import flamelink from 'flamelink/app';

import 'flamelink/content';

const flApp = flamelink({
    firebaseApp,
    dbType: 'cf',
    env: 'production',
    locale: 'en-US',
});

export const testFunction = functions.https.onRequest((request, response) => {
    flApp.content.get({
        schemaKey: 'projects'
    }).then(
        (res: any) => {
            console.log(res) // This return 'undefined'
            response.send(res)
        }
    ).catch(
        (error: any) => response.send(error)
    )
});

Help please.. been stuck on this for days.

dewetvdm commented 4 years ago

Hi @ribalnasr

Does this work if you run it server-side as opposed to a cloud function?

I will have a look at it and see if I can reproduce it within a cloud function.

ribalnasr commented 4 years ago

i haven't tried that. i usually only work server-side within a firebase project. i will try to reproduce it with node as soon as i can. meanwhile awaiting your reply

On Wed, Jul 22, 2020 at 4:07 PM De Wet van der Merwe < notifications@github.com> wrote:

Hi @ribalnasr https://github.com/ribalnasr

Does this work if you run it server-side as opposed to a cloud function?

I will have a look at it and see if I can reproduce it within a cloud function.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/flamelink/flamelink-js-sdk/issues/136#issuecomment-662441722, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGFEVDOEM7H2LY77SR77N7TR43P7NANCNFSM4PEVV6OA .

ribalnasr commented 4 years ago

i just tried outside firebase and it's working! any updates from your side?

neilpoulin commented 4 years ago

I use Flamelink in Firebase Cloud Functions and don't have any issues. I noticed that in my setup, I'm importing Flamelink like this, vs what you shared (however this could just be a difference in typescript settings)

// How my app is set up
import * as flamelink from "flamelink/app";

// your example
import flamelink from 'flamelink/app';

You also may need to get your admin app using the admin.app() function, rather than the return of admin.initializeApp - at least that's how my code has been running for months. See below.

import * as admin from "firebase-admin";

admin.initializeApp();
const firebaseApp = admin.app();
// then initialize flamelink like you had
dewetvdm commented 4 years ago

Thanks @Neilpoulin I think that is exactly the issue.

Might be good to add an example on the docs for this. @ribalnasr to summarize


import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as flamelink from 'flamelink/app';
import 'flamelink/content';

admin.initializeApp();
const firebaseApp = admin.app();

const flApp = flamelink({
  firebaseApp,
  dbType: 'cf',
  env: 'production',
  locale: 'en-US',
});

export const testFunction = functions.https.onRequest((request, response) => {
  ....
});
ribalnasr commented 4 years ago

Unfortunately this still doesn't work.

here are my packages versions:

"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.1",
"flamelink": "^1.0.0-alpha.31",

and firebase-tools is version 8.6.0

what are yours?

ribalnasr commented 4 years ago

I finally got it working! I was emulating the firebase project using firebase emulators:start This was emulating a local/empty version of Firestore hence flamelink.content.get() was returning empty values!!!

It worked when I ran firebase emulators:start --only functions.

Another important thing to note: i tried to use the same logic for flamelink.storage.getUrl(...) but got SigningError. This was fixed when explicitly adding the service account to firebase admin as follows:

const serviceAccount = require("./path/to/service-account.json");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: '...',
    storageBucket: '...'
});
gitdubz commented 4 years ago

Thanks, we will add a section to the docs for this 👍