Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"self" is not defined when trying to use getfunctions/httpscallable API from firebase #100

Closed
dahburj opened this issue Jun 14, 2021 · 5 comments

Comments

@dahburj
Copy link

dahburj commented Jun 14, 2021

Describe the bug
Well I don't if it's a bug or if I'm doing something wrong, but I keep getting all these crazy errors and build fails when I try to use/import firebase functions from any client side script. I have a multitude of Firebase Functions (besides the svelte kit one) that I'd like to utilize, but I have no idea how, any help would be greatly appreciated.

To Reproduce
Steps to reproduce the behavior:

  1. Add import { getFunctions, httpsCallable } from 'firebase/functions'; to any client side script (even firebase/compat/functions gave the same following errors)
  2. Run npm run build
  3. See error
    Expected behavior
    A build success message and the ability to use firebase's httpscallable API on some client rendered scripts, like for signup/login

Screenshots

Additional context
Error message below:
Adapter configuration:
{"firebaseJson":"firebase.json","sourceRewriteMatch":"**"}
Writing Cloud Function server assets to:
functions/sveltekit
Erasing static asset dir before processing:
public
Writing client application to:
public
Prerendering static pages to:
public

self is not defined
ReferenceError: self is not defined
at file:///Users/jd/Documents/github/svelte-adapter-firebase_example/.svelte-kit/output/server/app.js:13520:30
at ModuleJob.run (node:internal/modules/esm/module_job:175:25)
at async Loader.import (node:internal/modules/esm/loader:178:24)
at async prerender (file:///Users/jd/Documents/github/svelte-adapter-firebase_example/node_modules/.pnpm/@sveltejs[email protected][email protected]/node_modules/@sveltejs/kit/dist/chunks/index7.js:80:14)
at async Object.prerender (file:///Users/jd/Documents/github/svelte-adapter-firebase_example/node_modules/.pnpm/@sveltejs[email protected][email protected]/node_modules/@sveltejs/kit/dist/chunks/index7.js:312:5)
at async adapt (file:///Users/jd/Documents/github/svelte-adapter-firebase_example/node_modules/.pnpm/svelte-adapter-firebase@0.8.4_@sveltejs[email protected]/node_modules/svelte-adapter-firebase/src/index.js:51:4)
at async adapt (file:///Users/jd/Documents/github/svelte-adapter-firebase_example/node_modules/.pnpm/@sveltejs[email protected][email protected]/node_modules/@sveltejs/kit/dist/chunks/index7.js:338:2)
at async file:///Users/jd/Documents/github/svelte-adapter-firebase_example/node_modules/.pnpm/@sveltejs[email protected][email protected]/node_modules/@sveltejs/kit/dist/cli.js:656:5

@dahburj dahburj closed this as completed Jun 14, 2021
@jthegedus
Copy link
Owner

Hey @dahburj, I haven't used the Client JS SDK in SvelteKit yet, but I believe you need to use it after checking that your are in a browser env with https://kit.svelte.dev/docs#modules-$app-env and then also onMount with https://svelte.dev/docs#onMount

Can you share the reason you closed this Issue?

@dahburj
Copy link
Author

dahburj commented Jun 15, 2021

I closed it because I got it to work within half an hour of posting the issue; the steps you mention was my OG game plan, however, let's just say it was a lot easier to plan than to execute😂.. But I finally found a way to get to import into the client side without the ton of errors I kept hitting. For any/all interested; I got it to work by using the router=browser variable in each of the functions, and; more importantly, to get it to run AND build without errors I had to import the firebase modules at the beginning of each/every function.

if (router) {
  const { getFunctions, httpsCallable } = await import("firebase/functions");
  const functionsN = getFunctions();
  await signUpWithEmailPassword(signupEmail, signupPassword);
  const fCreateUser = httpsCallable(functionsN, "createUser");

  await fCreateUser({ name: userFullName })
    .then(async (result) => {
      console.log(result);
      const data = result.data;
      console.log(data);
      notificationMessage.set({
        message: "Welcome back!",
        type: "toast--link",
      });
      disableAction = false;
      await goto("/dashboard");
    })
    .catch(function (error) {
      console.log(error);
    });
}

P.S. I reached many-a similar problem trying to import firestore, and as my process went auth, functions, firestore, I felt defeated and settled on writing out my store middleware as a serious of firebase https functions, it works fine and I feel as though with bundling and caching maybe it won't be that bad, nonetheless, any advice/input you may have on the matter will be appreciated greatly🙋🏻‍♂️

@jthegedus
Copy link
Owner

I will probably write a guide on Firebase with SSR tools once I go through the motions of getting it to work in my own apps. I currently only have the new firebase-admin@exp lib working in SvelteKit Endpoints, so no need for trickery checking runtimer (browser/server) or waiting for component mounts.

@dahburj
Copy link
Author

dahburj commented Jun 16, 2021

That sounds amazing, I feel as though I came close to the same thing but my experience with svelte-kit started when I found this adapter (in all seriousness twas a God send). My train of thought was to convert my datastore functions (now in the server running as httpscallable and using the firebase-admin@exp) into something along the lines of data bundles , setting each's timeout/cache accordingly and also a nice endpoint, but like I said, my experience with svelte-kit is pretty much nil; plus, I cant say I was/am too keen on diving into it because of the fact that using that method would entail changing up the "rewrites" in firebase.json and I'm not tring messing with the beauty of what your adapter does at this point in my development road map .😂 Do let me know when/if you decide to share those Sveltekit Endpoints!

@jthegedus
Copy link
Owner

I will add examples to the docs as I resolve the issues - #101

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants