I'm trying to delete some data from firebase realtime database after some time but gives me this error: Uncaught TypeError: ref2.orderByChild is not a function
this is the code:
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.8.1/firebase-app.js";
import {getDatabase, ref, child, onValue, get} from "https://www.gstatic.com/firebasejs/9.8.1/firebase-database.js"
import "https://cdn.jsdelivr.net/npm/seamless-scroll-polyfill#latest/lib/bundle.min.js";
const firebaseConfig = {
apiKey: "xxxxx",
authDomain: "xxxxx",
databaseURL: "xxxxx",
projectId: "xxxxx",
storageBucket: "xxxxx",
messagingSenderId: "xxxxx",
appId: "xxxxx"
};
const app = initializeApp(firebaseConfig);
const db = getDatabase();
var ref2 = ref(db, "/2021-2022/Pentamestre/Voti/Novità");
var now = Date.now();
var cutoff = now - 2 * 60 * 60 * 1000;
var old = ref2.orderByChild('timestamp').endAt(cutoff).limitToLast(1);
var listener = old.on('child_added', function(snapshot) {
snapshot.ref.remove();
});
Obviously in firebaseConfig the datas are presents, i only hidden them here
There's a top level function query() in the new Modular SDK to build a Query. Similarly, orderByChild() and other query constraints are also a function. Try refactoring the code as shown below:
var old = query(ref2, orderByChild('timestamp'), endAt(cutoff), limitToLast(1));
var listener = onChildAdded(old, function(snapshot) {
// ...
});
I see you imported "get" but didnt use it,
and the ref "/2021-2022/Pentamestre/Voti/Novità" is maybe
"2021-2022/Pentamestre/Voti/Novità"
as path doesnt start with /slash
Related
I'm getting the error:
Uncaught FirebaseError: Invalid document reference. Document references must have an even number of segments, but todos has 1.
I wrote code because I want to inquire the data in the DB.
No matter how much I searched, I couldn't find the answer. which part is wrong
this is my firebaseConfig code:
import firebase from "firebase/compat/app";
import "firebase/compat/firestore";
import "firebase/compat/auth";
const firebaseConfig = {
apiKey: "***",
authDomain: "***",
projectId: "***",
storageBucket: "***",
messagingSenderId: "***",
appId: "***",
measurementId: "***",
};
firebase.initializeApp(firebaseConfig);
const firestore = firebase.firestore();
export { firestore };
export const auth = firebase.auth();
export const apiKey = firebaseConfig.apiKey;
this is code of firebaseController.tsx:
export const todos = firestore.collection('todos');
and this is code of view component:
useEffect(() => {
onSnapshot(todos, (snapshot: QuerySnapshot<DocumentData>) => {
setTodoItems(
snapshot.docs &&
snapshot.docs.map((doc) => {
return {
id: doc.id,
...doc.data(),
};
})
);
});
}, []);
my database:
DB
The onSnapshot method works on Firebase Documents, not on Firebase Collections. The first argument to onSnapshot you give i.e. "todos" is a collection reference but in reality, it expects a document reference, which you obtain by using the method
const docRef = doc(db, "path/to/document/inside/todos/collection")
onSnapshot(docRef, ......)
Hope this helps!
Whenevr I open the debugger in safari I get an error saying that undefined is not an object (evaluating 'firebase.initializeApp') and it points toward firebase.initializeApp(firebaseConfig);.
var firebase
let firebaseConfig = {
apiKey: "removed",
authDomain: "removed",
projectId: "removed",
storageBucket: "removed",
messagingSenderId: "removed",
appId: "removed"
};
firebase.initializeApp(firebaseConfig);
let db = firebase.firestore();
firebase.initializeApp is the v8 (and below) way of initializing the app. You could consider using the v9 (and above) style like this:
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.8.3/firebase-app.js";
const firebaseConfig = {
//...
};
const app = initializeApp(firebaseConfig);
More details on initialization upgrades to v9 here.
Or, using the Compat version (compatability with v8)
import firebase from "https://www.gstatic.com/firebasejs/9.8.4/firebase-app-compat.js"
firebase.initializeApp({ /* config */ });
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
Using html/JS, I am trying to write a file.
I included this on html head :
<head>
<script src="https://www.gstatic.com/firebasejs/5.7.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.7.0/firebase-firestore.js"></script>
Then at the end of the body I have :
//...
<script src="javascripts/main.js"></script>
<script>
var config = {
apiKey: "xxx",
authDomain: "xxxx.firebaseapp.com",
databaseURL: "https://xxxx.firebaseio.com",
projectId: "xxxx",
storageBucket: "xxxx.appspot.com",
messagingSenderId: "xxxxx"
};
firebase.initializeApp(config);
const db = firebase.firestore();
db.settings({timestampsInSnapshots:true});
</script>
Then on the main.js file when I try to write to storgae :
var storageRef = firebase.storage().storage.ref()('me/' + file.name);
var task=storageRef.put(file);
task.on('state_changed',
function progress(snapshot){console.log(snapshot.bytesTransferred)},
function error(err){},
function complete(){});
I get this error on the console :
firebase.storage is not a function
EDIT:
Tried again and got same error with :
// Create a root reference
var storageRef = firebase.storage().ref();
// Create a reference to 'images/mountains.jpg'
var finalRef = storageRef.child('me/' + file.name);
finalRef.put(file).then(function(snapshot){
console.log('uploading');
});
you need to add this also:
<script src="https://www.gstatic.com/firebasejs/5.7.0/firebase-storage.js"></script>
to be able to use firebase storage api.
Also change this:
var storageRef = firebase.storage().storage.ref()('me/' + file.name);
Into this:
var storageRef = firebase.storage().ref('me/' + file.name);
Check this for more info:
https://firebase.google.com/docs/storage/web/create-reference
change you script main.js position like this
<script>
var config = {
apiKey: "xxx",
authDomain: "xxxx.firebaseapp.com",
databaseURL: "https://xxxx.firebaseio.com",
projectId: "xxxx",
storageBucket: "xxxx.appspot.com",
messagingSenderId: "xxxxx"
};
firebase.initializeApp(config);
const db = firebase.firestore();
db.settings({timestampsInSnapshots:true});
</script>
//to here
<script src="javascripts/main.js"></script>
Check whether your firebase import is correct. It won't work if you imported it incorrectly.
It should be like this
import * as firebase from "firebase/app";
Not this
import * as firebase from "firebase";
I want to set up a Firebase Real-time Database connection from my Java backend to my JavaScript frontend, but the messages are not pushed to the client.
The backend Java code looks like this:
FirebaseOptions options = new FirebaseOptions.Builder()
.setServiceAccount(getServletContext().getResourceAsStream("/WEB-INF/xxx.json"))
.setDatabaseUrl("https://xxx.firebaseio.com")
.build();
FirebaseApp.initializeApp(options);
DatabaseReference ref = FirebaseDatabase
.getInstance()
.getReference();
ref.child("users").setValue("aaa");
My javascript code on the frontend looks like this:
var config = {
apiKey: "xxx",
authDomain: "xxx.firebaseapp.com",
databaseURL: "https://xxx.firebaseio.com",
storageBucket: "xxx",
messagingSenderId: "xxx"
};
firebase.initializeApp(config);
var database = firebase.database();
var myRef = firebase.database().ref("users");
myRef.on('value', function(snapshot) {
alert(5);
});
Is there something wrong with this code or why are the messages are not pushed? Am I missing something?