lmcq / firebase-firestorm

Firebase Firestorm is an ORM for Firestore which can be used with Typescript.
MIT License
252 stars 19 forks source link

TypeError: Cannot read property 'Timestamp' of undefined #11

Open davidassigbi opened 4 years ago

davidassigbi commented 4 years ago

Hi firestormers i'm having an issue (i think). Basically the TimesTamps class provided is causing some troubles to my project. Below is a screenshot of the error 2019-12-27-110425_1600x900_scrot

Here is a code snippet. Any help would be really appreciated..

import app from 'firebase/app';
import 'firebase/auth';
import 'firebase/firestore';
import 'firebase/messaging';
import { rootCollection, field, timestamp, Entity, ITimestamp } from 'firebase-firestorm';
import * as firestorm from 'firebase-firestorm';
import 'reflect-metadata';

@rootCollection({ name: 'users' })
export default class User extends Entity {
    @timestamp({
        name: 'created_at',
        updateOnCreate: true
    })
    createdAt!: ITimestamp;

    @timestamp({
        name: 'modified_at',
        updateOnUpdate: true
    })
    modifiedAt!: ITimestamp;

    @field({ name: 'nom' })
    name!: string;

    @field({ name: 'prenom' })
    firstname!: string;

    @field({ name: 'email' })
    email!: string;

    @field({ name: 'username' })
    username!: string;
}
const firebaseConfig = {/** firebase configuration object */}
const createFakeUser = () => ({/** some fake data provided by faker library */})
app.initializeApp(firebaseConfig)
app.auth()
app.firestore()
firestorm.initialize(app.firestore())
app.firestore().collection('users_azaz').add(createFakeUser())// even this passed so firebase is initialized successfully
firestorm.Collection(User).create(createFakeUserForFirestorm())// here is where it breack !!!

const createFakeUserForFirestorm = () => {
    const u = new User()
    const t = app.firestore.Timestamp.now()
    u.createdAt = new firestorm.Timestamp(t.seconds, t.nanoseconds)
    u.modifiedAt = new firestorm.Timestamp(t.seconds, t.nanoseconds)
    u.name = faker.name.findName()
    u.firstname = faker.name.findName()
    u.email = faker.internet.email()
    u.username = faker.internet.userName()
    console.log(u.toData())
    return u
}
rodrigorhas commented 4 years ago

Looking at the source code, i was thinking why we should wrap the native timestamp when we can simply use the native one instead... this is really the best approach?

davidassigbi commented 4 years ago

I keep on trying different workarounds but none was working. So as i was using a different version of firebase (latest) than the one that has been used in this firestorm version i was using and this fixed the issue. This fixed the issue for me however i wasn't really satisfied cause if i wanted to use some features only available in that latest version of firebase I would have been forced to give up using firestorm for that feature of firebase.

davidassigbi commented 4 years ago

And @rodrigorhas you're right. I don't think it is much usefull to wrap the native TimesTamp cause actually the wrapped version doesn't provide so much features however it is not a bad idea cause it is a good starting point for adding more and more features. And that the library API is still consistant there will not be a breaking change at any moment regarding the timestamp types.

davidassigbi commented 4 years ago

Right now i don't close the issue just cause i think my workaround is not the best one... I'll keep it open till it found a real solution.

wesleytian commented 4 years ago

Having a similar problem. I think it's because timestamp is represented as {"_seconds": 0, "_nanoseconds"}. (You can see if you print it out using JSON.stringify()).

But you can't access those properties by using ._seconds either. Very strange.

Screen Shot 2020-05-09 at 10 24 14 AM