Read data from Realtime Database

    0

    6

    Khalid Khan

    Firebase Smart Code Snippets

    The code imports the db module and exports a function that takes two arguments: a request object and a response object.

    If the request object doesn't have an id query parameter, the function returns a response with a status code of 400 and an error message containing the word "Missing."

    If the request does have an id query parameter, the function fetches the requested table's ref (object) and returns a reference to that ref. The function then uses that reference to execute a once clause that takes two arguments: a snapshot (object) and a function that returns a response.

    The function calls the the ref's once() method with the snapshot argument as the first parameter and the function that handles the response as the second parameter. The once() method returns a response with a status code of 200 and a JSON object that contains the following properties:

    total: The value of the snapshot.

    Shortcut: firebase.web.realtimedb

    import db from '../../lib/db-admin';
    
    export default (req, res) => {
      if (!req.query.id) {
        return res.status(400).json({
          error: 'Missing "id" query parameter'
        });
      }
    
      const ref = db.ref('table').child(req.query.id);
    
      return ref.once('value', (snapshot) => {
        res.status(200).json({
          total: snapshot.val()
        });
      });
    };
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.