Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4826

Record was saved to the server, but the response returned the new id 'null'. The store cannot assign a new id to a record that already has an id

$
0
0

@paisleydarts wrote:

Hi, I’m currently building a portfolio app that talks to a mongoDB API. I wrote the API and it’s fairly simple, this is the POST route

router.put('/:id', (req, res, next) => {
    console.log(req.body);
    Study.findByIdAndUpdate(req.params.id, req.body, {
        new: true,
        upsert: true
    }, (err) => {
        if (err) {
            err.status = 400;
            return next(err)
        }
        res.json({ message: 'Updated!' });
    });
});

I believe I’m setting up the proper Adapter and Serializer:

import DS from 'ember-data';
export default DS.RESTAdapter.extend(DS.BuildURLMixin, {
    namespace: 'api/v1',
    host: 'http://localhost:5000',
    updateRecord(store, type, snapshot) {
        let data = {
       
        };
    
    let serializer = store.serializerFor(type.modelName);
    serializer.serializeIntoHash(data, type, snapshot);
    debugger;

    let { id } = snapshot;
    let url = this.buildURL(type.modelName, id, snapshot, 'updateRecord');
    console.log(data.title);

 

    return this.ajax(url, 'PUT', { data: data });
  }

   import DS from 'ember-data';

   export default DS.RESTSerializer.extend({
    normalizeResponse(store, primaryModelClass, payload, id, requestType) {
        payload = {
            "studies": payload
        };

        return this._super(store, primaryModelClass, payload, id, requestType);
    },
    primaryKey: '_id'
    
});

and my controller and template seem to be right:

<h2>Edit</h2>
<form onSubmit={{action "updateTitle"}}>
    {{input value=model.title}}
    <button type="submit">Update</button>
</form>

import Controller from '@ember/controller';

export default Controller.extend({
    actions: {
        updateTitle(e) {
            e.preventDefault();
            let title = this.get('model');
            title.save()
            this.transitionToRoute('case-studies');
        }
    }
});

The record saves, but I get this error in my browser console:
Assertion Failed: 'study:5cdb30ca2fcde3c9ba7974ce' was saved to the server, but the response returned the new id 'null'. The store cannot assign a new id to a record that already has an id.

I’m still somewhat new to ember, I can’t see what I’m doing wrong.

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4826

Trending Articles