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

Saving a record to json file

$
0
0

Hi,

I’m building a small application just for me. So first I wanted to test on a small app what I can do with Ember Data, but I’m stuck on saving a record to json file. At the moment the json file is very simple, it has two records, and it reads:

{
    "data": [
        {
            "type": "person",
            "id": "albert-einstein",
            "attributes": {
                "name": "Albert Einstein",
                "field": "Gravity",
                "address":"Switzerland"
            }
        },
        {
            "type": "person",
            "id": "freeman-dyson",
            "attributes": {
                "name": "Freeman Dyson",
                "field": "Quantum Field Theory",
                "address": "Australia"
            }
        }
    ]
}

I’ve also defined a model:

export default class PersonModel extends Model {
    @attr('string') name;
    @attr('string') field;
    @attr('string') address;
}

On one page of the app, I load the data from the store and display it without problems. However, on another page I create a new record and I can see it in the store (and in Ember Inspector) but when saving the record I get an error.

For simplicity, I added the creation and saving of the new record in the route so whenever I click the link it creates a new record.

export default class InputRoute extends Route {
    async model() {
        let newRecord = this.store.createRecord('person', {
            name: 'Enrico Fermi',
            field: 'Quantum Physics',
            address: 'Italy'
        });

        newRecord.save();

        return newRecord;
    }
}

But I systematically get the following error:

index.js:64 Uncaught (in promise) TypeError: Cannot read property 'replace' of undefined
    at Cache.func (index.js:64)
    at Cache.get (index.js:761)
    at decamelize (index.js:167)
    at Cache.func (index.js:32)
    at Cache.get (index.js:761)
    at Object.dasherize (index.js:190)
    at normalizeModelName (-private.js:62)
    at detectMerge (-private.js:661)
    at IdentifierCache.updateRecordIdentifier (-private.js:468)
    at Store.didSaveRecord (-private.js:8774)

I’m really not sure where to look, the guides only show reading and displaying the data. I’m using the default serializer

export default class ApplicationSerializer extends JSONAPISerializer {
}

because the json file is in the appropriate format. I’m assuming something this simple should just work, so I must be missing something obvious but I can’t figure it out.

Thanks,

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 4870

Trending Articles