jeudi 27 août 2015

Ember save() throws JSON.parse error: "unexpected end of data at line 1 column 1 of the JSON data"

I have a model record created and being saved through a route and controller. When I save the record through the controller (via a savePlace action), I am seeing this error in the JS console:

SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

I've tried not setting anything on the model as well as setting dummy data on the model, but I get the same error. I am also user ember-cli http-mocks as a test backend to handle JSON responses. I realize it may be the response, but I'm not sure how else to configure the response.

Here's the relevant code:

routes/places/new.js:

import Ember from 'ember';

export default Ember.Route.extend({
  model: function() {
    return this.store.createRecord('place');
  },
});

controllers/places/new.js:

import Ember from 'ember';

export default Ember.Controller.extend({
  actions: {
    saveGeom(geom) {
      this.get('model').set('geometry', geom);
    },
    savePlace(data) {
      this.get('model').set('name', this.get('name')).set('description', this.get('description'));
      this.get('model').save().then(function() {
          alert("SUCCESS");
        }, function(error) {
          console.log(error);
        });
    }
  }
});

server/mocks/place.js:

  placeRouter.post('/places', function(req, res) {
    res.setHeader('Access-Control-Allow-Methods', 'POST');
    res.send({
      "places": {
            id: 1,
            name: "Triangle",
            description: "Ryan Christiani",
            geometry: {
              "type": "Polygon",
              "coordinates": [
                [
                  [-84.32281494140625,34.9895035675793],
                  [-81.73690795898438,36.41354670392876],
                  [-83.616943359375,  34.99850370014629],
                  [-84.05639648437499,34.985003130171066],
                  [-84.22119140625,   34.985003130171066],
                  [-84.32281494140625,34.9895035675793]
                ]
              ]
            }
        }
    });
  });

Thanks!




Aucun commentaire:

Enregistrer un commentaire