title: Reading data from firebase functions
body: <p>I've now spent the better part of 16 hours tearing my hair out over javascript, so I figured it was time to ask someone.</p>
  
  <p>I have an android app made in android studio, which is connected to a firebase database that looks roughly like this: </p>
  
  <pre><code>nameOfDatabase
      Places
          Bar
              B001
                  place_ID_ : "B001"
                  ...
                  average_rating_ : 0
                  ...
                  number_of_ratings_ : 0
  
              ...other bars
  
          ...other place types  
  
      Ratings
          ratingID1
              place_ID_ : "B001"
              rating_ : 3
              user_ID_ : 4315732985097
  
              ...other parameters
  
          ...other ratings
  </code></pre>
  
  <p>What I want to do is simply update the number_of_ratings_ and average_ratings_ for the place matching the place_ID_ whenever a rating_ changes, either by an existing value being modified or a new Rating being added. My index.js code for the firebase functions node module looks like this: </p>
  
  <pre><code>const functions = require('firebase-functions');
  const admin = require('firebase-admin');
  admin.initializeApp(functions.config().firebase);
  
  exports.UpdateAverageRatings = functions.database.ref('/Ratings/{rating_uid}/rating_').onWrite(event =&gt; {
  
      let place_ID_ = admin.database().ref('Ratings/' + String(event.params.rating_uid) + '/place_ID_').val();
  
      console.log(place_ID_);
  
      let place_type_ = 'BARs';
      let place_ID_ = 'B001';
      let avg_rating_ = 0;
      let accumulated_rating_ = 0;
      let number_of_ratings_ = 0;
  
      //Object.keys(place_ID_.parent).forEach(function(key) {
      //    if (place_ID_.parent[key] == 'place_ID_'){
      //        accumulated_rating_ += place_ID_.parent[key].value();
      //        number_of_ratings_ += 1;
      //}});
  
      let place_change_ = admin.database().ref('Places/' + String(place_type_) + '/' +String(place_ID_));
  
      place_change_.child('average_rating_').set(avg_rating_);
      place_change_.child('number_of_ratings_').set(number_of_ratings_);
  
      return 0;
  
    });
  </code></pre>
  
  <p>Setting the values, in the last two lines before return, works as it should, but for now I'm just setting the dummy values '0'. An idea for assigning the correct values is commented out for later use.</p>
  
  <p><strong>The problem is that whatever I try, I can't seem to read any values from the database, as seen attempted in the 'let place_ID_ ...' line and subsequent console log.</strong></p>
  
  <p>I've read through the documentation (<a href="https://firebase.google.com/docs/functions/database-events" rel="nofollow noreferrer">https://firebase.google.com/docs/functions/database-events</a>) and stackexchange posts countless times, but I either get</p>
  
  <p>"TypeError: admin.database(...).ref(...).val is not a function"</p>
  
  <p>in the firebase console logs , or if I remove val(); </p>
  
  <pre><code>W {
    u: 
     dh {
       app: 
        FirebaseApp {
          firebaseInternals_: [Object],
          services_: [Object],
          isDeleted_: false,
          name_: '[DEFAULT]',
          options_: [Object],
          database: [Function: bound ],
          INTERNAL: [Object] },
       L: 
        Tb {
          host: 'projectname.firebaseio.com',
          domain: 'firebaseio.com',
          Pc: true,
          ne: 'projectname',
          tg: false,
          ef: '',
          Za: 'projectname.firebaseio.com' },
       Ua: Ff { qc: {} },
       Sc: null,
       ca: he { tb: [] },
       td: 1,
       Qa: 
        kh {
          id: 0,
          f: [Function],
          nd: {},
          '$': {},
          pa: [],
          Lc: 0,
          Hc: [],
          ma: false,
          Ra: 1000,
          qd: 300000,
          Eb: [Function: bound ],
          Gc: [Function: bound ],
          se: [Function: bound ],
          L: [Object],
          xe: null,
          Ab: null,
          Ea: null,
          mb: null,
          Xc: [Object],
          ae: false,
          he: 0,
          Rd: undefined,
          sb: [Object],
          Kb: true,
          Ed: {},
          kg: 0,
          Oe: true,
          je: null,
          xc: null },
       va: 
        kh {
          id: 0,
          f: [Function],
          nd: {},
          '$': {},
          pa: [],
          Lc: 0,
          Hc: [],
          ma: false,
          Ra: 1000,
          qd: 300000,
          Eb: [Function: bound ],
          Gc: [Function: bound ],
          se: [Function: bound ],
          L: [Object],
          xe: null,
          Ab: null,
          Ea: null,
          mb: null,
          Xc: [Object],
          ae: false,
          he: 0,
          Rd: undefined,
          sb: [Object],
          Kb: true,
          Ed: {},
          kg: 0,
          Oe: true,
          je: null,
          xc: null },
       qg: Df { qf: {}, Sc: [Object], va: [Object] },
       jc: Vc { sd: '', Mc: null, A: [Object] },
       fe: Nb { Hd: [Object] },
       md: Dg { wa: [Object], hb: [Object], Be: {}, fc: {}, zc: [Object] },
       ia: Eb { B: null, k: null },
       Xa: ch { ta: [Circular], ba: [Object], INTERNAL: [Object] },
       cd: 0,
       ge: null,
       K: Dg { wa: [Object], hb: [Object], Be: {}, fc: {}, zc: [Object] } },
    path: G { o: [ 'Ratings', 'ratingID1', 'place_ID_' ], Y: 0 },
    m: 
     jf {
       xa: false,
       ka: false,
       Ib: false,
       na: false,
       Pb: false,
       oa: 0,
       kb: '',
       bc: null,
       xb: '',
       Zb: null,
       vb: '',
       g: Ae {} },
    Kc: false,
    then: undefined,
    catch: undefined }
  </code></pre>
  
  <p>, which is rather incredible given that this database key only holds an integer value. </p>
  
  <p>Any help would be greatly appreciated, I can provide more information if necessary.</p>
  
  <p><strong>Edit</strong>
  I've now tried using the async/await keywords coupled with once('value).then(snapshot => snapshot.val()) to get the value, but this does not compile, or if I remove async/await, the promise never resolves.</p>