Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js save() after createRecord sends empty req.body to my express.js server

When I use createRecord and then save, the express server receives the post request but the req.body is empty. I made the same post request using Postman and it works perfectly. The get request works too in the tasks template displaying the tasks in a list.

This is my ember code:

Route (tasks.js):

import Ember from 'ember';

export default Ember.Route.extend({
  model() {
    return this.store.findAll('task');
  },
  actions: {
   addTask() {
     const task = this.store.createRecord('task', {
       title: this.get("controller.title")
     });

     task.save();

   }
 }
});

Model (task.js):

import Model from 'ember-data/model';
import DS from 'ember-data';


export default Model.extend({
  title: DS.attr('string')
});

Template (tasks.hbs):

{{input value=title}}
<button id="add-task" {{action "addTask"}}>Add</button>

<br><br>

{{#each model as |task|}}
    {{task.title}}<br>
{{/each}}

{{outlet}}

I'm using the default ember JSONAPIAdapter.

like image 390
Jonathan Navarrete Santini Avatar asked Mar 13 '23 10:03

Jonathan Navarrete Santini


1 Answers

Ember and JSONAPIAdapter is sending the header with type application/vnd.api+json. I got it working by adding this in express.js:

app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
like image 132
Johannes Millegård Avatar answered Mar 14 '23 23:03

Johannes Millegård