Not able to add data using ember-data Model

323 views Asked by At

Controller/new-customer

  actions: {
    addInfo: function() {
      var name = this.get('name');
      var e_mail = this.get('e-address');
      var emoney = this.get('e-money');
      var newCustomer = this.store.createRecord('customers', {
        name    : name,
        email   : e_mail,
        emoney  : emoney
      });
      newCustomer.save();
      alert("You\'re added");
    }

Model/customers.js

import DS from 'ember-data';

export default DS.Model.extend({
    name:DS.attr('string'),
    email:DS.attr('string'),
    emoney:DS.attr('number'),
    rev: attr('string')
});

templates/new-customer.hbs

<form class="form-horizontal">
    <div class="form-group control-group error">
        <label class="control-label" for="inputError">Name</label>
        {{input type="text" class="form-control" value=name placeholder = "Enter your name" size="15"}}
    </div>
    <div class="form-group">
        <label>Email Address</label>
        {{textarea type="text" class="form-control" value=e-address placeholder = "Enter your Email Address"}}
    </div>
    <div class="form-group">
        <label>E-Money</label>
        {{input type="number" class="form-control" value=e-money}}
    </div>
    <button {{action 'addInfo'}}type="button" class="btn btn-primary">Submit !!</button>
</form>

Am I missing something? Error I get on the console while clicking the submit button is:

Failed to load resource: the server responded with a status of 404 (Not Found) vendor.js:27458 Ember Data Request POST /customers returned a 404 Payload (text/html; charset=utf-8) Cannot POST /customers

Error at AdapterError (http://localhost:4200/assets/vendor.js:83957:16) at Class.handleResponse (http://localhost:4200/assets/vendor.js:85238:14) at ajaxError (http://localhost:4200/assets/vendor.js:85736:25) at Class.hash.error (http://localhost:4200/assets/vendor.js:85310:23) at fire (http://localhost:4200/assets/vendor.js:3637:31) at Object.fireWith [as rejectWith] (http://localhost:4200/assets/vendor.js:3767:7) at done (http://localhost:4200/assets/vendor.js:9576:14) at XMLHttpRequest. (http://localhost:4200/assets/vendor.js:9816:9) defaultDispatch @ vendor.js:27458

1

There are 1 answers

0
Trek Glowacki On

It appears Ember Data is working correctly but your API server is not running or configured correctly.

newCustomer.save(); attempts to save the record to your API and the error is explaining the exact issue:

Failed to load resource: the server responded with a status of 404
(Not  Found) vendor.js:27458 Ember Data Request POST /customers
returned a 404 Payload (text/html; charset=utf-8) Cannot POST /customers

Attempting to POST data to /customers on your API is resulting in a 404 at the server.