Can't Access Firebase Database Outside Of My Wifi - javascript

For some reason I can only read and write in the firebase realtime database when I am connected to the wifi that I created the database on. Do any know a reason as to why this is happening?
I tried to debug the issue, however, my console log is not showing any details when I run the code that pulls a query. (assuming my credentials are correct)
import * as firebase from "firebase";
var config = {
apiKey: "********",
authDomain: "********",
databaseURL: "********",
projectId: "********",
storageBucket: "********",
messagingSenderId: "*******"
};
if (!firebase.apps.length) {
firebase.initializeApp(config);
console.log("initialized");
}
componentDidMount(){
console.log('before query function call');
firebase.database().ref().child('messages/').once('value', function (snapshot) {
console.log('in query function call');
console.log(snapshot.val())
});
expected results in console log
---initialized
---before query function call
---in query function call
---(snapshot of data in realtime database)
actual results in console log
---initialized
---before query function call

Related

"firebase is not defined" error using the current vr. of firebase

I tried making a simple chat forum using YouTube tutorial. upon its release it works wonderful so I tried using its code structure while updating the core infos and it only result in an error. I tried many time to avail, I'm new to programming so im looking for help.
<script>src="https://www.gstatic.com/firebasejs/9.16.0/firebase-app.js"</script>
<script>src="https://www.gstatic.com/firebasejs/9.16.0/firebase-database.js"</script>
<script>
var firebaseConfig = {
apiKey: "AIzaSyCTBkpUWMU_pL6qIb_hMXVDS_sCYQ2BROU",
authDomain: "rd-try-ce276.firebaseapp.com",
databaseURL: "https://rd-try-ce276-default-rtdb.firebaseio.com",
projectId: "rd-try-ce276",
storageBucket: "rd-try-ce276.appspot.com",
messagingSenderId: "47874427531",
appId: "1:47874427531:web:819f34b203e6ae82c0628e"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
console.log(firebase);
function sendMessage() {
alert("Hello! I am an alert box!");
//get message
var message = document.getElementById("message").value;
//save in database
firebase.database().ref("messages").push().set({
"message": message
})
}
</script>

Get sms verification code sent to user in javascript sent from Firebase signInWithPhoneNumber function

below piece of code works perfectly fine
// Initialize Firebase
var config = {
apiKey: "muykjkjgh........",
authDomain: "my-project.firebaseapp.com",
databaseURL: "https://my-project.firebaseio.com/",
projectId: "my-project",
storageBucket: "my-project.firebaseio.com",
messagingSenderId: "8235......"
};
firebase.initializeApp(config);
window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('verify', {
'size': 'invisible',
'callback': function(response) {
}
});
firebase.auth().signInWithPhoneNumber("+911234567890", window.recaptchaVerifier)
.then(function(confirmationResult) {
window.confirmationResult = confirmationResult;
//I want to achieve 6 digit code sent to users mobile number
//log doesnt show
console.log(confirmationResult);
});
};
As above code I have commented is there a way to extract a 6 digit code sent to user mobile number

Firebase realtime database not returning any data

I am trying to retrieve an object the i added to my firebase realtime database But i am not getting anything and i dont know why . i made sure to implement and call firebase correctly .
This is the response i get one i cal the database .
and this is my imlimentation
<script>
var firebaseConfig = {
apiKey: "*",
authDomain: "*",
databaseURL: "*",
projectId: "*",
storageBucket: "*",
messagingSenderId: "*",
appId: "*"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
var dbRef = firebase.database();
var contactsRef = dbRef.ref("survey");
console.log(contactsRef)
</script>
You need to attach a listener to retrieve the data:
var dbRef = firebase.database();
var contactsRef = dbRef.ref("survey");
contactsRef.once('value').then(function(snapshot) {
console.log(snapshot.val());
});
snapshot.val() will give you all the data under node survey.
For more information check this:
https://firebase.google.com/docs/database/web/read-and-write#read_data_once

Firebase data retrieval to web

// Initialize Firebase
var config = {
apiKey: "AIzaSyBz13eirmEOGD1uXOZwf6tKGnEsjfwsUFo",
authDomain: "platformtechproject.firebaseapp.com",
databaseURL: "https://platformtechproject.firebaseio.com",
projectId: "platformtechproject",
storageBucket: "platformtechproject.appspot.com",
messagingSenderId: "1065758005439"
};
firebase.initializeApp(config);
var database = firebase.database();
ref msgs clllection
var msgRef = firebase.database().ref('survey');
I am trying to connect to firebase, but I continue getting these errors on chrome console failing to connect to firebase:
Chrome console error
This is because you did not write:
<script src="https://www.gstatic.com/firebasejs/4.12.0/firebase.js"></script>
in the <head>...</head>
You have to write it to be able to use the firebase sdk in your project.
more info here:
https://firebase.google.com/docs/web/setup

FCM Cannot receive messages on WebApp

Well, simply put, I cannot receive messages via my WebApp using JS. I am receiving tokens and I am able to send messages via Firebase console to the WebApp's token, but I am not getting any receiving.
Here is my JS Code:
var config = {
apiKey: "AIzaSyBLCsBQVXLsDjPhjdkvJVMGyDUcfWrFtQU",
authDomain: "php-test-project-78c66.firebaseapp.com",
databaseURL: "https://php-test-project-78c66.firebaseio.com",
projectId: "php-test-project-78c66",
storageBucket: "php-test-project-78c66.appspot.com",
messagingSenderId: "856365479454"
};
firebase.initializeApp(config);
$(document).ready(function(){
const messaging = firebase.messaging();
messaging.requestPermission().then(function() {
console.log("Permitted");
return messaging.getToken();
}).then(function(token) {
console.log(token);
}).catch(function(error) {
console.log("Not Permitted");
});
messaging.onMessage(function(payload) {
console.log("TEST");
console.log("Message received. ", payload);
});
});
Upon sending message via Firebase Console, I do not receive anything. I send by using TokenID.
PS: The keys are of a Dev Testing App, hence I kept it in. If admins consider this a problem then you may remove it.

Categories

Resources