dimanche 2 juillet 2017

Ember + Firebare: Getting property generated by Could Function when saving record completes

I use a Cloud Function to generate a short unique URL on a record on the 'onWrite' event, and save it. This works well, but when I save a record from my Ember app using EmberFire, I do get a model back as an argument to a callback, but the URL of this model is undefined. Is there a way to return this back to the client? Or do I need to query the record to get the generated URL?

This is how my Clound Function code looks:

exports.generateUrl = functions.database.ref('/tournaments/{tid}')
  .onWrite(event => {
    if (event.data.previous.exists()) {
      return;
    }
    if (!event.data.exists()) {
      return;
    }

    const url = shortid.generate();

    return event.data.ref.update({ url });
  });

Here is my component that saves data through form submission. I'm using an add-on called ember-changeset to handle some validations, but this shouldn't be related to the issue.

export default Ember.Component.extend({

  submit(e) {
    e.preventDefault();

    let snapshot = this.changeset.snapshot();

    return this.changeset
      .cast(Object.keys(this.get('schema')))
      .validate()
      .then(() => {
        if (this.changeset.get('isValid')) {
          return this.changeset
            .save()
            .then((result) => {
                // Here, result.get('url') is undefined.
            })
        }
      })
  }
});




Aucun commentaire:

Enregistrer un commentaire