Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vuex-ORM is improperly handling date fields. #624

Open
jpoiri opened this issue Apr 24, 2020 · 5 comments · May be fixed by #653
Open

Vuex-ORM is improperly handling date fields. #624

jpoiri opened this issue Apr 24, 2020 · 5 comments · May be fixed by #653
Labels
bug Something isn't working
Milestone

Comments

@jpoiri
Copy link

jpoiri commented Apr 24, 2020

Describe the bug

When I insert a date through Vuex-ORM, the date is stored as empty object instead of a Date object (see below), however when I insert a date through Vuex directly its stored correctly

With Vuex-ORM:

image

With Vuex:

image


Steps to reproduce the bug

  1. Define model with a date
import { Model } from '@vuex-orm/core';

/**
 * This class represent a Member
 */
export default class Member extends Model {
  static entity = 'members'

  static loaded = false

  /**
   * Member fields
   */
  static fields() {
    return {
      id: this.string(),
      firstName: this.string(null),
      lastName: this.string(null),
      gender: this.string(null),
      smoker: this.boolean(null),
      birthDate: this.attr(null),
    }
  }
}
  1. Configure Vuex
import Vue from 'vue'
import Vuex from 'vuex'
import VuexORM from '@vuex-orm/core'
import Member from '@/models/Member'

// Vuex modules
import i18n from './modules/i18n'

// Create new instance of Database.
const database = new VuexORM.Database()

database.register(Member)

Vue.use(Vuex)

export default new Vuex.Store({
  modules: {
    i18n,
  },
  state: {
    member: null,
  },
  mutations: {
    SET_MEMBER(state, member) {
      state.member = member
    },
  },
  plugins: [VuexORM.install(database)],
})
  1. Create data
import Vue from 'vue'
import Component from 'vue-class-component'
import Member from '@/models/Member'

@Component({ name: 'MembersIndex' })
export default class MembersIndex extends Vue {
  /**
   * Created hook
   */
  async created() {
    Member.insert({
      data: {
        id: 1,
        firstName: 'John',
        lastName: 'Doe',
        gender: 'M',
        birthDate: new Date(),
      },
    })
    this.$store.commit('SET_MEMBER', {
      id: 1,
      firstName: 'John',
      lastName: 'Doe',
      gender: 'M',
      birthDate: new Date(),
    })
  }
}
  1. See error

Expected behaviour

The date object should be correctly stored in Vuex when using Vuex-ORM

Versions

  • Vuex ORM: 0.36.3
  • Vue: 2.6.10
  • Vuex: 3.0.1
@cuebit cuebit added the bug Something isn't working label Apr 27, 2020
@cuebit cuebit added this to the v1.0.0 milestone Apr 27, 2020
@mtrunt mtrunt linked a pull request Jun 17, 2020 that will close this issue
9 tasks
@Tofandel
Copy link

Tofandel commented Jul 21, 2020

Any chance to have this bug fixed before v1 as this is currently a major bug that makes dates unusable and the fix is not breaking and very low complexity?

@McAnix
Copy link

McAnix commented Nov 4, 2020

This is a major issue on my implementation too. I import masses of objects from the server through Vuex-ORM Axios and all the dates are invalid, even when using the date plugin.

@scripterkaran
Copy link

This is a deal breaker for me :/ , I am also facing this same problem

@brenthmiras
Copy link

I stumbled upon a similar issue, where I'm trying to store an instance of my custom es6 class.

@brenthmiras
Copy link

What if we pass () => () => new Date("Your date string") to the insert. When we retrieve, it will return instanceof Date.
`

  Member.insert({

      data: {
        id: 1,
        firstName: 'John',
        lastName: 'Doe',
        gender: 'M',
        birthDate: () => () => new Date(),
      },

  })

`
Because, functions will not satisfy typeof === 'object' which is why dates are converted to object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants