0

I am trying to keep some data consistent with firebase cloud functions. When data changes in the main list, I want all the data to change in the user's favourite list.

Currently, I am able to call the function and get a log of the correct data which is changing, but my query doesn't work. I am getting the following error:

TypeError: ref.update is not a function at exports.itemUpdate.functions.database.ref.onUpdate

Here is my code:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.itemUpdate = functions.database
  .ref('/items/{itemId}')
  .onUpdate((change, context) => {

  const before = change.before.val();  // DataSnapshot after the change
  const after = change.after.val();  // DataSnapshot after the change

  console.log(after);

  if (before.effects === after.effects) {
    console.log('effects didnt change')
    return null;
  }
  const ref = admin.database().ref('users')
    .orderByChild('likedItems')
    .equalTo(before.title);

  console.log(ref);

  return ref.update(after);
});

I'm not to sure where I am going wrong, I appreciate all the help and guidance to resolve this!

Cheers.

hugger
  • 426
  • 4
  • 19
  • See https://stackoverflow.com/a/40592759, https://stackoverflow.com/a/45059154, https://stackoverflow.com/a/45047781, https://stackoverflow.com/a/42105112 – Frank van Puffelen Nov 17 '18 at 20:59

1 Answers1

1

equalTo() returns a Query object. You're then trying to call update() on that object. Note that in the linked API docs, Query doesn't have an update() method. You can't simply "update" a Query that hasn't been performed. You're going to have to actually perform the query using once(), iterate the results form the snapshot in the returned promise, and perform further updates using the data you find.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441