# Saving a record to json file

**URL:** <https://discuss.emberjs.com/t/saving-a-record-to-json-file/18207>\
**Category:** Ember Data\
**Created:** [September 14, 2020, 11:46am UTC](https://discuss.emberjs.com/t/saving-a-record-to-json-file/18207 "2020-09-14T11:46:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkmkmk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/mkmkmk/32/16864_2.png) [@mkmkmk](https://discuss.emberjs.com/u/mkmkmk)\
**Post date:** [September 14, 2020, 11:46am UTC](https://discuss.emberjs.com/t/saving-a-record-to-json-file/18207/1 "2020-09-14T11:46:33Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![dknutsen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dknutsen/32/16471_2.png) [@dknutsen](https://discuss.emberjs.com/u/dknutsen)\
**Post date:** [September 14, 2020, 2:36pm UTC](https://discuss.emberjs.com/t/saving-a-record-to-json-file/18207/2 "2020-09-14T14:36:44Z")

</div>

Hi @mkmkmk, welcome! I think the problem is mostly that Ember Data is meant to work with an API and writing a file isn’t going to give it everything it expects. Ember Data is highly configurable so you could probably get this to work… but there are probably also better options.

The super rentals tutorial (if you’ve seen it) uses static files for fixtures because they’re easy to start with and they work really well for _read_ operations. Unfortunately when you start trying to _write_ data things get a little more complicated. Ember Data expects a POST/PUT/PATCH response to contain a full record. The API is always the source of truth for data and so when you write a record with Ember Data it wants the full record back so it makes sure the store is up to date with the real record from the backend. So in this case if you’re writing to a file your “API” probably isn’t functioning how Ember Data expects. And actually if you’re just using the default adapter there’s nothing to handle saving the file anyway… so I’m not even sure what this would do…

I would highly recommend using [mirage](https://github.com/miragejs/ember-cli-mirage) for mocking an API. It’s much more sophisticated and works _really_ well with Ember Data. You can use Fixtures for static data, or you can create Factories and dynamically create data in a “scenario”. It also allows you to define “resources” and/or specific PUT/PATCH/POST handlers so your “API” will behave just like a real one (aka how Ember Data expects).

If your end goal is really to save data to local disk I’d consider using local storage or indexedDB instead of raw files. There are addons for both ([local storage](https://github.com/funkensturm/ember-local-storage) and [indexedDB](https://github.com/mydea/ember-indexeddb) that provide Ember Data adapters.

---

<div class="post-metadata">

**Author:** ![mkmkmk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/mkmkmk/32/16864_2.png) [@mkmkmk](https://discuss.emberjs.com/u/mkmkmk)\
**Post date:** [September 16, 2020, 6:48am UTC](https://discuss.emberjs.com/t/saving-a-record-to-json-file/18207/3 "2020-09-16T06:48:26Z")

</div>

Hi Dan,

Thanks for the in-depth response, now I understand much better what is going on. I’ll go with your suggestion then.

Thanks again
