Receive JSON from local Server (mocks)


#1

Hi everyone !! :sunglasses:

I’m sorry but after 3 days searching this ****** solution I GIVE UP !! :cry:

So my WISH is : use google-distance with EMBER !! :heart_eyes:

1 - Solution : :x: Google-distance use fs module, so it’s make impossible to use :

import distance from 'npm:google-distance' 

Already try !

2 - Solution: :ballot_box_with_check: So now my goal is to create a little mocks http server with Ember, and this server will import the google-distance module play with it and send the JSON repsonse I want to my Ember App !!!

So for now, I succeed to deploy my server, and I have a JSON (manual for now) when I connect to

localhost:4200/api/v1/distances

BUT !! My problems IS —> I can’t or don’t know HOW to get back and UUSSEEEE my JSON. Ok let’s see some code !!

My server : server/mocks/distance.js

distancesRouter.get('/', function (req, res) {
	var distance = {
		"data": {
			"type": "distance",
			"id": "1",
			"attributes": {
				"kmDistance": "Sam",
				"timeDistance": "Selikoff"
			}
		}
	};
	res.set('Content-Type', 'application/vnd.api+json');
	res.send(distance);
});

==> So here, when I GET http://localhost:4200/api/v1/distances I see in my browser :

{"data":{"type":"distance","id":"1","attributes":{"kmDistance":"Sam","timeDistance":"Selikoff"}}}

So it’s working !! Also try with postman !

Ok and here is my config :

app/adapters/distance.js:

import JSONAPIAdapter from 'ember-data/adapters/json-api';

export default JSONAPIAdapter.extend({
  namespace: 'api/v1',
  host: 'http://localhost:4200'
});

app/model/distance.js :

import DS from 'ember-data';

export default DS.Model.extend({
    kmDistance: DS.attr('string'),
    timeDistance: DS.attr('string')
});

app/routes/expedition/new.js :

actions: {
	saveExpedition(newExpedition) {
		var model = this.store.findAll('distance').then((distance) => {
			distance.forEach(function (meta, name) {
				console.log(meta);
			});

		});
		newExpedition.save().then(() => {
			this.transitionTo('expedition');
		});
	},

==> Here is the page when I want to add a expedition to my databse (I use firebase). So for now it just to try, but after the goal is :

The customer put the start and end of the expedition, I send it to my server, it look for the distance with google-distance then send me back a JSON file with the kmDistance and timeDistance. So I can fulfill the progression attribute of my expedition ( for example, 5km/minute and I fake the progression of my expedition)

But now my problem is that I THINK I receive the JSON because when I (in app/routes/expedition/new.js)

console.log(meta);

here is what I found :

But IMPOSSIBLE to find my kmDistance & timeDistance in it :sob: :sob: :sob: :sob:

PLEASE !!! help me :slight_smile: And sorry for my english :wink: