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
42 stars 5 forks source link

Cannot read property 'get' of null #50

Closed gabber235 closed 5 years ago

gabber235 commented 5 years ago

When running a get query it doesn't work.

Imports: `import flamelink from 'flamelink/app'; import 'flamelink/content'; import 'flamelink/storage'; import 'flamelink/schemas';

const firebaseApp = admin.initializeApp(functions.config()); const app = flamelink({ firebaseApp, dbType: 'cf', precache: false });`

Query: await app.content.get({ schemaKey: schema, entryId: id, populate: true }) the schema is homePage and the id is 2RVNgcqGUuRMOkB5aB3j

Error: TypeError: Cannot read property 'get' of null at isRefLike (/user_code/node_modules/flamelink/node_modules/@flamelink/sdk-utils/dist/cjs/index.js:7250:24) at /user_code/node_modules/flamelink/node_modules/@flamelink/sdk-utils/dist/cjs/index.js:7456:54 at step (/user_code/node_modules/flamelink/node_modules/@flamelink/sdk-utils/dist/cjs/index.js:80:23) at Object.next (/user_code/node_modules/flamelink/node_modules/@flamelink/sdk-utils/dist/cjs/index.js:61:53) at /user_code/node_modules/flamelink/node_modules/@flamelink/sdk-utils/dist/cjs/index.js:54:71 at __awaiter (/user_code/node_modules/flamelink/node_modules/@flamelink/sdk-utils/dist/cjs/index.js:50:12) at /user_code/node_modules/flamelink/node_modules/@flamelink/sdk-utils/dist/cjs/index.js:7400:75 at process._tickDomainCallback (internal/process/next_tick.js:135:7)

What is going wrong?

jperasmus commented 5 years ago

Hi @gabber235

Is homePage a collection or a single type of schema? If it is a single, you only need to specify the schemaKey.

gabber235 commented 5 years ago

It's a single type, so I will try that.

jperasmus commented 5 years ago

Great, let me know how that goes for you. I will also take a look if there is a way to make the SDK code check a bit more robust for this scenario.

gabber235 commented 5 years ago

Hey @jperasmus,

Thanks for your quick reply today. I have changed the entryId to const entryId = schemaType === 'single' ? undefined : id and I get undefined which I run in app.content.get({ schemaKey: schema, entryId: entryId, populate: true }) but I still get the same error

jperasmus commented 5 years ago

Okay, I will deploy a newer version that will explicitly check for this use case, which should hopefully resolve this issue for you. I'll let you know as soon as it is available.

jperasmus commented 5 years ago

v1.0.0-alpha.8 is now available. Can you upgrade to it and test whether you're still experiencing this issue?

gabber235 commented 5 years ago

Now I have got this error

Error: Bucket name not specified or invalid. Specify a valid bucket name via the storageBucket option when initializing the app, or specify the bucket name explicitly when calling the getBucket() method. at FirebaseError.Error (native) at new FirebaseError (/user_code/node_modules/firebase-admin/lib/utils/error.js:42:28) at Storage.bucket (/user_code/node_modules/firebase-admin/lib/storage/storage.js:107:15) at Object.ref (/user_code/node_modules/flamelink/node_modules/@flamelink/sdk-storage/dist/cjs/cf/index.js:5218:22) at Object.<anonymous> (/user_code/node_modules/flamelink/node_modules/@flamelink/sdk-storage/dist/cjs/cf/index.js:5460:44) at step (/user_code/node_modules/flamelink/node_modules/@flamelink/sdk-storage/dist/cjs/cf/index.js:77:23) at Object.next (/user_code/node_modules/flamelink/node_modules/@flamelink/sdk-storage/dist/cjs/cf/index.js:58:53) at fulfilled (/user_code/node_modules/flamelink/node_modules/@flamelink/sdk-storage/dist/cjs/cf/index.js:48:58) at process._tickDomainCallback (internal/process/next_tick.js:135:7)

Registration code: const firebaseApp = admin.initializeApp(functions.config()); const app = flamelink({ firebaseApp, dbType: 'cf', precache: false });

jperasmus commented 5 years ago

This sounds like an issue with the Firebase admin app instance. Can you try and log out functions.config() so we can see what you have in there? You can also try and initialize your admin app without functions.config()

gabber235 commented 5 years ago

The functions.config() == {} and when I run it without I get this error

Error: A Forbidden error was returned while attempting to retrieve an access token for the Compute Engine built-in service account. This may be because the Compute Engine instance does not have the correct permission scopes specified. Identity and Access Management (IAM) API has not been used in project 676984383187 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/iam.googleapis.com/overview?project=676984383187 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry. at GaxiosError (/user_code/node_modules/firebase-admin/node_modules/gaxios/build/src/common.js:17:9) at Gaxios.<anonymous> (/user_code/node_modules/firebase-admin/node_modules/gaxios/build/src/gaxios.js:72:27) at next (native) at fulfilled (/user_code/node_modules/firebase-admin/node_modules/gaxios/build/src/gaxios.js:16:58) at process._tickDomainCallback (internal/process/next_tick.js:135:7)

jperasmus commented 5 years ago

Are you running this code on your local machine or from within Firebase Cloud Functions?

gabber235 commented 5 years ago

From within the firebase cloud functions on a machine from Google

jperasmus commented 5 years ago

Okay, this is not an issue with this Flamelink SDK anymore, but with the Firebase admin app initialization. This Flamelink SDK does not dictate how the firebaseApp instance is created, as long as it is valid to do what is needed. In this case, the firebaseApp instance does not have access to the storage bucket.

You will need to make sure that the way it is initialized, it includes the storageBucket property. Something like this:

var admin = require("firebase-admin");

var serviceAccount = require("path/to/serviceAccountKey.json");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://<PROJECT_ID>.firebaseio.com",
    storageBucket: "<BUCKET_NAME>.appspot.com"
});
gabber235 commented 5 years ago

Thanks for helping me, now everything works.

jperasmus commented 5 years ago

It's a pleasure, I'm glad it is working now!