Firebase problem

Hello friends i'm trying to add firebase cloud messaging to test firebase and try to implement something easy into a small button but i just keep getting problems after problem i used npm to install firebase and it keeps saying errors when i try to import it then i ended up importing it from link which worked at first but wouldn't let the whole code process now any help with be appreciated i've been trying to follow every tutorial and guid but i even copy and pasted the code they have and it didn't work
36 Replies
ABK | Muneer
ABK | MuneerOP•2y ago
the problem i face in this code is the last line
ABK | Muneer
ABK | MuneerOP•2y ago
ABK | Muneer
ABK | MuneerOP•2y ago
and after i installed firebase using npm this is the error i have trying to use the normal thing
ABK | Muneer
ABK | MuneerOP•2y ago
Jochem
Jochem•2y ago
what bundler are you using?
ABK | Muneer
ABK | MuneerOP•2y ago
npm also been trying all kind of cdn but still wouldn't even let me console.log(firebase)
Jochem
Jochem•2y ago
npm's the package manager, I mean vite or something like it?
ABK | Muneer
ABK | MuneerOP•2y ago
ABK | Muneer
ABK | MuneerOP•2y ago
vanilla js No framework just normal javascript
Jochem
Jochem•2y ago
ah, nm, I didn't realize you could import from a URL nowadays in vanilla js
ABK | Muneer
ABK | MuneerOP•2y ago
yah lol they let you do that i use some libraries like that sometime
ABK | Muneer
ABK | MuneerOP•2y ago
ABK | Muneer
ABK | MuneerOP•2y ago
that's from firebase i'm pretty new to fire base this is like the first time i even try it and idk if there's something i must do before using it the thing now i'm looking at fireship video talking about basic firebase setup and he can console.log firebase and get an object while i can't
ABK | Muneer
ABK | MuneerOP•2y ago
ABK | Muneer
ABK | MuneerOP•2y ago
but for me it shows that https://i.imgur.com/3CFWvty.png
Imgur
Jochem
Jochem•2y ago
I only played around with it a little myself, and that was using vite and npm... the 404 errors are weird, I don't see any references in your code to locally hosted scripts for firebase
ABK | Muneer
ABK | MuneerOP•2y ago
Can i know how to add that? I'm looking at fireship video and followed what he did and other videos like him
Jochem
Jochem•2y ago
I gotta admit, I've never set up vite myself, it always just comes along with sveltekit or whatever other framework I'm using <_< Can you copy/paste this so I can try running it locally without re-typing everything?
ABK | Muneer
ABK | MuneerOP•2y ago
Okie
ABK | Muneer
ABK | MuneerOP•2y ago
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.17.1/firebase-app.js"; // TODO: Add SDKs for Firebase products that you want to use // https://firebase.google.com/docs/web/setup#available-libraries // Your web app's Firebase configuration const firebaseConfig = { apiKey: "AIzaSyDqV0x1Ghsm51vauGm5-IJBcOKXagOKWdg", authDomain: "gfattentionbutton.firebaseapp.com", databaseURL: "https://gfattentionbutton-default-rtdb.europe-west1.firebasedatabase.app", projectId: "gfattentionbutton", storageBucket: "gfattentionbutton.appspot.com", messagingSenderId: "939746855769", appId: "1:939746855769:web:1e3334ecdd63d00c849a48" }; // Initialize Firebase const app = initializeApp(firebaseConfig); console.log(firebase)
ABK | Muneer
ABK | MuneerOP•2y ago
I think the first thing i need to do is be able to console.log firebase
Jochem
Jochem•2y ago
firebase isn't defined anywhere in that script, so it's not available to be logged.
ABK | Muneer
ABK | MuneerOP•2y ago
ahh correct
Jochem
Jochem•2y ago
he's not quite pasting what you pasted here, I think he's also including these two
ABK | Muneer
ABK | MuneerOP•2y ago
i'm just trying everything to fix it lol first one is to call the firebase so i can use it without importing it like this( import { initializeApp } from "https://www.gstatic.com/firebasejs/9.17.1/firebase-app.js";) and the second one is to call the function i want to use which is cloud messaging
Jochem
Jochem•2y ago
it's also a 2 year old tutorial, it might not work exactly the same anymore
ABK | Muneer
ABK | MuneerOP•2y ago
yah but i've been stuck in the same problem for the past 2 days and i'm trying everything at this point tbh so all the code now worked beside one line
Jochem
Jochem•2y ago
might be best to try from scratch using nothing but the firebase docs, mixing and matching tutorials is never a good idea, especially when there's two major versions between the tutorial and the one you're using
ABK | Muneer
ABK | MuneerOP•2y ago
import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.17.1/firebase-app.js';
import { getMessaging, getToken } from 'https://www.gstatic.com/firebasejs/9.17.1/firebase-messaging.js';




const firebaseConfig = {
apiKey: "AIzaSyDqV0x1Ghsm51vauGm5-IJBcOKXagOKWdg",
authDomain: "gfattentionbutton.firebaseapp.com",
databaseURL: "https://gfattentionbutton-default-rtdb.europe-west1.firebasedatabase.app",
projectId: "gfattentionbutton",
storageBucket: "gfattentionbutton.appspot.com",
messagingSenderId: "939746855769",
appId: "1:939746855769:web:1e3334ecdd63d00c849a48"
};

const app = initializeApp(firebaseConfig);


const messaging = getMessaging(app);



getToken(messaging, {vapidKey:"BLnYjSJhf2uwwYOJYzKmeP283Fpxlq4gGaYj41cPLa79w6BMOkuzLn1Ic-g-cYfahEFFwzSvwki4i3DG_b7HJNU"})
import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.17.1/firebase-app.js';
import { getMessaging, getToken } from 'https://www.gstatic.com/firebasejs/9.17.1/firebase-messaging.js';




const firebaseConfig = {
apiKey: "AIzaSyDqV0x1Ghsm51vauGm5-IJBcOKXagOKWdg",
authDomain: "gfattentionbutton.firebaseapp.com",
databaseURL: "https://gfattentionbutton-default-rtdb.europe-west1.firebasedatabase.app",
projectId: "gfattentionbutton",
storageBucket: "gfattentionbutton.appspot.com",
messagingSenderId: "939746855769",
appId: "1:939746855769:web:1e3334ecdd63d00c849a48"
};

const app = initializeApp(firebaseConfig);


const messaging = getMessaging(app);



getToken(messaging, {vapidKey:"BLnYjSJhf2uwwYOJYzKmeP283Fpxlq4gGaYj41cPLa79w6BMOkuzLn1Ic-g-cYfahEFFwzSvwki4i3DG_b7HJNU"})
i console.log the variables and it all works beside the last one on i put what they said in the docs and still get the error
Jochem
Jochem•2y ago
it looks like firebase-messaging requires a local script to exist, maybe it's further down in the docs?
ABK | Muneer
ABK | MuneerOP•2y ago
ABK | Muneer
ABK | MuneerOP•2y ago
ABK | Muneer
ABK | MuneerOP•2y ago
it's the second step and they ask to replace the key with what i have in firebase i apologize if that's a rookie thing to ask but it just keeps giving errors
Jochem
Jochem•2y ago
seems like there's a bunch of extra steps: https://stackoverflow.com/questions/63799227/fcm-gettoken-failed-to-register-a-serviceworker-for-scope-error-flutter-web/63800226#63800226 it's also 2 years old, but might still be useful I wouldn't know what the problem is otherwise. It seems like it expects a firebase-messaging-sw.js file to exist, but I don't personally know why or have any further experience with firebase. I hope either the stack overflow answer helps, or someone else peeks in and can answer
ABK | Muneer
ABK | MuneerOP•2y ago
thank you for your time and effort ! appreciate it!
Jochem
Jochem•2y ago
no worries 🙂
Want results from more Discord servers?
Add your server