Home > Mobile >  ref2.orderByChild is not a function
ref2.orderByChild is not a function

Time:05-16

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

CodePudding user response:

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) {
    // ...
});

CodePudding user response:

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