0
votes

Using Firebase Cloud Functions I am trying to expand a uri that is in a firestore document and replace it with the expanded uri.

I am using the tall npm package (https://www.npmjs.com/package/tall) which is working well, I just am not able to get the resulting expanded uri into my object to put back into firestore.

I believe its not returning before the rest of my function finishes and therefore not giving me the data. When I tried using the example on the page for async and use await firebase gives an error.

I am assuming I am missing something very simple but after a solid day uploading to cloud functions, testing, and trying again I am beyond frustrated.

What am I missing?

exports.addonSanitized = functions.firestore
  .document('addons/{addonId}')
  .onCreate(doc => {
    const addonId = doc.id;
    const addon = doc.data();

    const expandedLink = tall(addon.link)
      .then(unshortenedUrl => console.log('Tall url', unshortenedUrl))
      .catch(err => console.error('AAAW ????', err));

    const sanitized = {
      summary: `${expandedLink}`
    };

    return admin
      .firestore()
      .collection('addons')
      .doc(addonId)
      .update(sanitized)
      .then(doc => console.log('Entry Sanitized', doc));
  });

I expect the expandedLink to return the expanded link. What is being inputed into the document is [object Promise]

1

1 Answers

0
votes

You're getting "[object Promise]" because the value of expandedLink is a Promise.

The value you actually want is unshortenedUrl. You can only access that value inside the then() where it exists, so you need to return the tall Promise instead, and have your other return statement inside then().

Potentially something like this (untested):

exports.addonSanitized = functions.firestore
  .document('addons/{addonId}')
  .onCreate(doc => {
    const addonId = doc.id;
    const addon = doc.data();

    return tall(addon.link)
      .then(unshortenedUrl => {
        console.log('Tall url', unshortenedUrl)
        const sanitized = {
          summary: `${unshortenedUrl}`
        };
        return admin
          .firestore()
          .collection('addons')
          .doc(addonId)
          .update(sanitized)
          .then(doc => console.log('Entry Sanitized', doc));
      })
      .catch(err => console.error('AAAW 👻', err));

  });