0

I am having trouble with simple EmberFire queries. I want to find a department object by name, and then add it to a college departments according to the data model described below.

The error I am receiving is:

TypeError: Cannot read property 'pushObject' of undefined

What am I doing wrong?

              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()

            }
          )

Where department is:

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

Here is my full query:

    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()

            }
          )
      })
},

Department:

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

College:

import DS from 'ember-data';

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

EDIT: c object from query:

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
Janusz Chudzynski
  • 2,700
  • 3
  • 33
  • 46
  • Are you sure "c" is returning an object from the query? You should check that first. console.log(c) then check to see if it prints to the console. I may be missing the departments. – talves Jan 17 '17 at 21:21
  • @talves please check updated answer. I printed the 'c'. It's a 'college' type. I assume it should have an empty array of departments if none are associated with it? – Janusz Chudzynski Jan 17 '17 at 21:29
  • Sorry, you might want to try console.log(college.get('departments')), it should be undefined. Maybe you meant college.get('department') singular. – talves Jan 17 '17 at 21:32
  • Please have look at the college model. I have 'departments' there. – Janusz Chudzynski Jan 17 '17 at 21:33
  • I have put my suggestion in an answer, let me know if that is what you are looking for. I am not sure what your `college` object looks like when it returns, so I am making assumptions. – talves Jan 17 '17 at 21:43

2 Answers2

1

You should try something similar to the following:

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
  • 13,993
  • 5
  • 40
  • 63
0

talves's answer will work in most cases, but it is a bit ugly. It is the commonly accepted way to solve this since EmberFire doesn't really offer anything better. This is, however, an add-on which will work for all of your querying needs. You can check it out here.

Tom
  • 350
  • 4
  • 21