Why is Object.get('key') undefined in Ember?

707 views Asked by At

javascript

So even if I define the keys and values using .set() I'm still getting object.get('key') is undefined. There is one example in the image above.

pojso.set({foo : 'bar'}) i still get pojso.get('foo') is undefined

it would make sense that if you set() something, you would be able to get() it, but, this is really weird.

Here are my unit test that kept failing with undefined error, I did google many times, It's not like i didn't do any research. I didn't know what to search for. The error messages I'm getting were vague, made me think my object structure wasn't right, or my syntax was wrong.

it 'renders', ->
  component = @subject()
  # component.set('row', { #undefined is not a constructor (evaluating 'this.get('row.schedules').reduce')
  #   'schedules' : {}
  # })
  # component.set('row', { #undefined is not a constructor (evaluating 'schedule.get('timeRanges')')
  #   'schedules' : [{
  #     timeRanges : [
  #       ['on': '0 0 * * 0', 'off': '1 1 * * 1'],
  #       ['on': '2 2 * * 2', 'off': '3 3 * * 3']
  #     ]
  #   }]
  # })
  component.set('row', { #undefined is not a constructor (evaluating 'schedule.get('timeRanges')')
    'schedules' : [
      {
        'timeRanges' : [
          {'on': '0 0 * * 0', 'onFrequency': 'Auto'},
          {'on': '1 1 * * 1', 'onFrequency': 'weekly'}
        ]
      },
      {
        'timeRanges' : [
          {'on': '2 2 * * 2', 'onFrequency': 'monthly'},
          {'on': '3 3 * * 3', 'onFrequency': 'daily'}
        ]
      },
    ]
  })
  # console.log('row.schedules.firstObject.timeRanges', component.get('row.schedules.firstObject.timeRanges'))
  # component.set('row', { # Type error reduce@[native code]
  #   'schedules' : [
  #     ['on': '0 0 * * 0', 'off': '1 1 * * 1'],
  #     ['on': '2 2 * * 2', 'off': '3 3 * * 3']
  #   ]
  # })
  # component.set('row', { #undefined is not a constructor (evaluating 'schedule.get('timeRanges')')
  #   'schedules' : [
  #     {'on': '0 0 * * 0', 'off': '1 1 * * 1'},
  #     {'on': '2 2 * * 2', 'off': '3 3 * * 3'}
  #   ]
  # })
  # component.set('row', { #undefined is not a constructor (evaluating 'this.get('row.schedules').reduce')
  #   'schedules' : {
  #     'timeRanges' : [
  #       {'on': '0 0 * * 0', 'off': '1 1 * * 1'},
  #       {'on': '2 2 * * 2', 'off': '3 3 * * 3'}
  #   ]}
  # })
  # component.set('row', { #undefined is not a constructor (evaluating 'this.get('row.schedules').reduce')
  #   schedules : {timeRanges : {}}
  # })
  # component.set('row', { #undefined is not a constructor (evaluating 'this.get('row.schedules').reduce')
  #   schedules : {}
  # })
  # component.set('row': {})
  # component.set('schedules', {#Assertion Failed: The key provided to set must be a string, you passed [object Object]
  #   'timeRanges' : {}
  # })
  # component.set('row.schedules', {#Assertion Failed: The key provided to set must be a string, you passed [object Object]
  #   'timeRanges' : [
  #     {'on': '0 0 * * 0', 'off': '1 1 * * 1'},
  #     {'on': '2 2 * * 2', 'off': '3 3 * * 3'}
  #   ]
  # })
  # component.set('row.schedules', {#Assertion Failed: The key provided to set must be a string, you passed [object Object]
  #   'timeRanges' : [
  #     {on: '0 0 * * 0', off: '1 1 * * 1'},
  #     {on: '2 2 * * 2', off: '3 3 * * 3'}
  #   ]
  # })
  # component.get('row').set('schedules', {#Assertion Failed: The key provided to set must be a string, you passed [object Object]
  #   timeRanges : [
  #     {on: '0 0 * * 0', off: '1 1 * * 1'},
  #     {on: '2 2 * * 2', off: '3 3 * * 3'}
  #   ]
  # })
  # component.set('row.schedules', {#Assertion Failed: The key provided to set must be a string, you passed [object Object]
  #   timeRanges : [
  #     {on: '0 0 * * 0', off: '1 1 * * 1'},
  #     {on: '2 2 * * 2', off: '3 3 * * 3'}
  #   ]
  # })
  @render()
  expect(component).to.be.ok
  expect(@$()).to.have.length(1)
1

There are 1 answers

1
Awesome_girl On

Ember objects are different from javascript objects. Instead of pojso = {foo: 'bar'} You should do Ember.Object.create({foo : 'bar'}) solution

source: Ember docs 'get'

source: Ember.object explanation