-2

I am working on an Ember application..

Its using Ember.inject.service modules -- and its not resolving the promise - not providing just the string I need from it

I have a service that looks like this

let downloadMethod = this.get('service1')
  .fetch(id)
  .then(res => {
    console.log("res", res.agenda.method.value)
    res.agenda.method.value
 });

console.log("downloadMethod", downloadMethod);

when I try to access this -- the console log for downloadmethod shows a promise -- but inside the then -- res comes out as the string value I need.

how do I get the value out of the "downloadMethod" -- it shows as a promise, instead of a string result?

Do I need to wrap this around an Ember.RSVP.hash?

The Old County
  • 89
  • 13
  • 59
  • 129

2 Answers2

2

The console.log("downloadMethod", downloadMethod) is called before the promise finishes, so you don't have the string value yet. You just have the unresolved promise.

So you need to save the value inside the then function. Below is some code to show what that looks like if this was inside an Ember Component

import Component from '@ember/component';
import { inject as service } from '@ember/service';

export default Component.extend({
  service1: service(),
  fetchedValue: null,

  actions: {
    someAction() {
      this.get('service1')
        .fetch(id)
        .then(res => {
          this.set('fetchedValue', res.agenda.method.value);
        });
    }
  }
})

This code is Ember version 2.16 and above

Paul
  • 18,349
  • 7
  • 49
  • 56
  • That's a possible way -- but I do need to obtain the fetchedvalue at the level of a call to action. – The Old County Apr 24 '18 at 18:17
  • @TheOldCounty I updated my code example to fetch the data in an action. – Paul Apr 24 '18 at 19:54
  • the problem is - this is only part of a very very complicated pre-built Ember application. I'm already going to have to hijack and prevent an action to run this promise -- and I'm more of a react js dev – The Old County Apr 24 '18 at 23:13
  • @Gaurav - I understand that - but the code is written in Ember. It appears several services are merged using RSVP – The Old County Apr 25 '18 at 08:14
-1

The actual solution to this was to create a return RSVP.hash in the model parent.

model () {
    return RSVP.hash({
      deliveryMethod: this.get('service1').fetch(this.get('service2').id).then(res => {
         return res.agenda.method.value;
      })
    });
},
The Old County
  • 89
  • 13
  • 59
  • 129
  • service2 was not mentioned in the original example code. Could you clean up the question so we can understand this answer? – Gaurav Apr 25 '18 at 19:16
  • @Gaurav it doesn't matter - this service2 was used to get an id - so really just an id is returned at that juncture -- "this.get('service2').id" -- its used to fetch a record - so what you have is a chain. I don't really know how else to explain it - given you have the exact code - and the problem throughout -- whenever I tried to access "downloadMethod" it would return a promise -- then just a fetched string. --- its a giant 50-100 file Ember project -- and so not even Paul's answer satisfies the issue for me. – The Old County Apr 25 '18 at 21:54