FirebaseExtended/emberfire

Bug in firestore: Cannot read property 'replace' of undefined

lalitindoria opened this issue ยท 5 comments

Version info

DEBUG: -------------------------------
DEBUG: Ember      : 3.18.1
DEBUG: Ember Data : 3.18.0
DEBUG: Firebase   : 7.0.0
DEBUG: EmberFire  : 3.0.0-rc.6
DEBUG: -------------------------------

I am trying to save a sample record in the following way (record gets saved but throws an error):

const game = this.store.createRecord('game', {
      id: Math.round(date.getTime() / 1000),
      has_started: 0,
      is_over: 0,
      player1: 1,
      player2: 2,
      player3: 3,
      player4: 4,
    });
await game.save();

This throws an error :

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:648)
    at IdentifierCache.updateRecordIdentifier (-private.js:456)
    at Store.didSaveRecord (-private.js:8724)

Model app/models/game.js

import Model, { attr } from '@ember-data/model';

export default class GameModel extends Model {
  @attr('boolean') hasStarted;
  @attr('boolean') isOver;
  @attr() player1;
  @attr() player2;
  @attr() player3;
  @attr() player4;
}

Adapter app/adapters/application.js

import FirestoreAdapter from 'emberfire/adapters/firestore';

export default FirestoreAdapter.extend({
});

Serializer app/serializers/application.js

import FirestoreSerializer from 'emberfire/serializers/firestore';

export default class ApplicationSerializer extends FirestoreSerializer {
}

This is reproducible with the bare minimum configuration.

I have this same error, although it only manifests when saving and using then.

Same error here.

To (temporarily) fix, rollback ember-data to 3.14.

@bnetter @mattmcginnis @lalitindoria You can use this patch in your app to keep it working again: #600 (comment)

./app/serializers/application.js

import RealtimeDatabaseSerializer from 'emberfire/serializers/realtime-database';

export default class ApplicationSerializer extends RealtimeDatabaseSerializer {

  // eslint-disable-next-line no-unused-vars
  normalizeCreateRecordResponse(_store, _primaryModelClass, payload, id, _requestType) {
    return { data: { id: id || payload.ref.key, attributes: payload.data, type: _primaryModelClass.modelName }};
  }
}

You can find an implementation here as well: https://github.com/zoltan-nz/library-app/blob/master/app/serializers/application.js

^ And for the Firestore serializer:

import FirestoreSerializer from 'emberfire/serializers/firestore';

export default class ApplicationSerializer extends FirestoreSerializer {
  normalizeCreateRecordResponse(_store, _primaryModelClass, payload, id, _requestType) {
    return { data: { id: id || payload.doc.id, attributes: payload.data, type: _primaryModelClass.modelName } };
  }
}