代码之家  ›  专栏  ›  技术社区  ›  Alex

如何使用javascript在firebase中进行过滤?

  •  0
  • Alex  · 技术社区  · 2 年前

    下面是我的javascript代码片段,它的响应显示了所有管理数据库,taufik,nanarsih,,,我想用admin键获取数据库值,我该怎么做?

    <script type="module">
    // Import the functions you need from the SDKs you need
    import {
        initializeApp
    } from "https://www.gstatic.com/firebasejs/9.6.11/firebase-app.js";
    import {
        getAnalytics
    } from "https://www.gstatic.com/firebasejs/9.6.11/firebase-analytics.js";
    import {
        getDatabase,
        ref,
        onValue,
        equalTo
    } from "https://cdnjs.cloudflare.com/ajax/libs/firebase/9.6.11/firebase-database.min.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
    // For Firebase JS SDK v7.20.0 and later, measurementId is optional
    const firebaseConfig = {
        apiKey: "123",
        authDomain: "123",
        databaseURL: "123",
        projectId: "123",
        storageBucket: "123",
        messagingSenderId: "123",
        appId: "123",
        measurementId: "123"
    };
    
    // Initialize Firebase
    const app = initializeApp(firebaseConfig);
    const analytics = getAnalytics(app);
    const db = getDatabase(app);
    
    onValue(ref(db), (snapshot) => {
        const data = snapshot.val();
        console.log(data);
    });
    

    In firebase

    这是控制台日志中的响应:

    {admin: 123, nanarsih: 123, taufik: 123}
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   puf - Frank van Puffelen    2 年前

    只是为了得到 admin 从数据库中,您可以创建对该路径的引用,然后读取:

    const adminRef = ref(db, 'admin');
    onValue(adminRef, (snapshot) => {
        const data = snapshot.val();
        console.log(data);
    });