新手Emberfire查询

Newbie Emberfire Querying

我在处理简单的 EmberFire 查询时遇到问题。 我想通过名称找到一个部门对象,然后根据下面描述的数据模型将它添加到一个大学部门。

我收到的错误是:

TypeError: Cannot read property 'pushObject' of undefined

我做错了什么?

              store.query('college', {orderBy: 'name', equalTo: "CAS"}).then(function(c){
              var college = c
              console.log(college);
              dept.set('college',college);
              college.get('departments').pushObject(dept);
              dept.save();
              college.save()

            }
          )

所在部门:

  store.query('department', {orderBy: 'name', equalTo: "Testing One"}).then(function(depts){
              // var dept = depts.get('content')[0]
              var dept = depts})

这是我的完整查询:

    findOne(){
      var store = this.store
      // store.query('department', {orderBy: 'name', equalTo: "Testing One"}).then(function(depts){
      store.query('department', {orderBy: 'name', equalTo: "Testing One"}).then(function(depts){
          // var dept = depts.get('content')[0]
          var dept = depts
          console.log(dept);
          //Add it to college
          store.query('college', {orderBy: 'name', equalTo: "CAS"}).then(function(c){
              var college = c//.get('content')[0]
              console.log(college);
              dept.set('college',college);
              college.get('departments').pushObject(dept);
              dept.save();
              college.save()

            }
          )
      })
},

部门:

export default DS.Model.extend({
   name: DS.attr('string'),
   faculty:DS.hasMany('faculty'),
   college: DS.belongsTo('college')
});

大学:

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  departments: DS.hasMany('department')
});

编辑:来自查询的 c 对象:

Class
__ember1484688357664
:
"ember632"
__ember_meta__
:
Meta
__firebaseCleanup
:
()
_super
:
ROOT()
_updatingPromise
:
null
content
:
(...)
get content
:
GETTER_FUNCTION()
set content
:
SETTER_FUNCTION(value)
isLoaded
:
true
isUpdating
:
false
links
:
EmptyObject
manager
:
Class
meta
:
EmptyObject
query
:
Object
store
:
Class
type
:
quality-online@model:college:
__proto__
:
Class

您应该尝试类似以下的操作:

store.query('department', {orderBy: 'name', equalTo: "Testing One"}).then(function(c) {
  return c.get('firstObject');
}).then(function(college) {
  departments = college.get('departments');
  departments.pushObject(dept);
  ...
});

talves 的答案在大多数情况下都有效,但有点难看。这是解决此问题的普遍接受的方法,因为 EmberFire 并没有真正提供更好的方法。然而,这是一个可以满足您所有查询需求的插件。你可以看看here.