Janusz Chudzynski
Janusz Chudzynski

Reputation: 2710

Newbie Emberfire Querying

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

Upvotes: 0

Views: 174

Answers (2)

Tom
Tom

Reputation: 400

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.

Upvotes: 0

talves
talves

Reputation: 14353

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);
  ...
});

Upvotes: 1

Related Questions