为什么在Ember中未定义Object.get('key')?

时间:2016-12-19 15:49:20

标签: javascript object ember.js getter

javascript

所以,即使我使用.set()定义键和值,我仍然得到object.get('key')未定义。上图中有一个例子。

pojso.set({foo : 'bar'}) 我还是得到了 pojso.get('foo') is undefined

如果你set()某事,你就可以get()它,这是有意义的,但是,这真的很奇怪。

以下是我的单元测试因undefined错误而失败,我多次谷歌,这不是我没有做任何研究。我不知道该搜索什么。我得到的错误信息含糊不清,让我觉得我的对象结构不正确,或者我的语法错误。

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 个答案:

答案 0 :(得分:2)

Ember对象与javascript对象不同。而不是pojso = {foo: 'bar'} 你应该做Ember.Object.create({foo : 'bar'}) solution

来源:Ember docs 'get'

来源:Ember.object explanation