我遇到了簡單的EmberFire查詢問題。 我想按名稱查找部門對象,然後根據下面描述的數據模型將其添加到大學部門。新手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
:
[email protected]:college:
__proto__
:
Class
你確定「c」從查詢中返回一個對象嗎?你應該先檢查一下。 console.log(c)然後檢查它是否打印到控制檯。我可能會錯過這些部門。 – talves
@talves請檢查更新的答案。我打印了'c'。這是一個「大學」類型。我認爲它應該有一個空的數組部門,如果沒有與它相關聯? –
對不起,您可能想嘗試console.log(college.get('departments')),它應該是未定義的。也許你的意思是college.get('部門')單數。 – talves