emberfire icon indicating copy to clipboard operation
emberfire copied to clipboard

v3 missing firebase.database.ServerValue.TIMESTAMP?

Open oskarrough opened this issue 6 years ago • 5 comments

In v2 you could do

import firebase from 'firebase'
firebase.database.ServerValue.TIMESTAMP

but in v3 ServerValue is not defined. What is the right import path? https://firebase.google.com/docs/reference/js/firebase.database.ServerValue.html

I tried accessing database.ServerValue from:

  • import firebase from 'firebase'
  • import firebase from 'firebase/app'
  • firebase: service()
  • firebaseApp: service()

oskarrough avatar Jul 21 '19 11:07 oskarrough

The proper way is:

import firebase from 'firebase/app';
firebase.database.ServerValue.TIMESTAMP

Note due to an outstanding bug with how the Firebase modules are defined this will throw an error in type-checking. But it's actually correct.

If yu don't care about Node.js support this will work:

import * as firebase from 'firebase/app';
firebase.database.ServerValue.TIMESTAMP

jamesdaniels avatar Oct 05 '19 23:10 jamesdaniels

Thanks, I tried both of your suggestions but I'm seeing this with rc.6?

index.cjs.js:1470 TypeError: Cannot read property 'ServerValue' of undefined
import DS from 'ember-data'
// import firebase from 'firebase/app'
import * as firebase from 'firebase/app'

const {Model, attr, belongsTo, hasMany} = DS

export default Model.extend({
	created: attr('number', {
		defaultValue() {
			return firebase.database.ServerValue.TIMESTAMP
		}
	}),
	channels: hasMany('channel'),
	settings: belongsTo('user-setting')
})

and compiled version

define("xxx/models/user", ["exports", "ember-data", "firebase/app"], function (_exports, _emberData, firebase) {
  "use strict";

  Object.defineProperty(_exports, "__esModule", {
    value: true
  });
  _exports.default = void 0;
  const {
    Model,
    attr,
    belongsTo,
    hasMany
  } = _emberData.default;

  var _default = Model.extend({
    created: attr('number', {
      defaultValue() {
        return firebase.database.ServerValue.TIMESTAMP;
      }

    }),
    channels: hasMany('channel'),
    settings: belongsTo('user-setting')
  });

  _exports.default = _default;
});

oskarrough avatar Oct 13 '19 11:10 oskarrough

interesting, wonder if auto-import is pruning some dependencies...

jamesdaniels avatar Oct 14 '19 01:10 jamesdaniels

I'm planning on adding new data types to the adapter that will do stuff like this for you, I'll leave open for that.

jamesdaniels avatar Oct 14 '19 01:10 jamesdaniels

Probably can be close with the Firebase Js V9 API !

  import { serverTimestamp } from 'firebase/database'

CICCIOSGAMINO avatar Sep 21 '21 14:09 CICCIOSGAMINO