2014-12-08 4 views
1

내 경로 중 하나에이 항목이 있습니다.왜 사용자 ID를 설정할 수 없습니까?

Error: Assertion Failed: Error: The backend rejected the commit because it was invalid: {userId: can't be blank} 

그리고 API 페이로드는 다음과 같습니다 : console.log(user.get('id'));user이어야 나타내는 사용자의 ID를 가져옵니다 때문에

{"order":{"user_id":null,"product_id":"30"}} 

그것은 이상한

var order = _this.store.createRecord('order'); 
order.set('product', product); 

this.store.find('userProfile', 1).then(function(user) { 
    console.log(user.get('id')); 
    order.set('user', user); 
}); 

order.save().then(function() { 
    _this.transitionTo('products.index'); 
}); 

나는이 오류 올바른 대상.

사용자 ID가 설정되지 않은 것으로 보입니다.

import DS from 'ember-data'; 

export default DS.Model.extend({ 
    user: DS.belongsTo('userProfile'), 
    product: DS.belongsTo('product') 
}); 

내가 뭔가를 놓친 건가 :

Order 모델처럼 보이는 어떤 correclty을 실행하지 않는 order.set('user', user);을 의미 ...?

+0

당신이 ActiveModel 어댑터를 사용하고 있습니까? – Sushant

+0

예, ActiveModelAdapter –

답변

1

this.store.find을 호출하면 비동기이며 약속이 반환됩니다. 이 코드는 find 바로 뒤에 order.save()을 호출하므로 약속이 아직 해결되지 않았으므로 사용자 레코드는 여전히 null입니다. 이것이 페이로드에 user_id이없는 이유입니다.

console.logthen 처리기 안에 있으며, 약속이 완료되면 실행되어 사용자의 속성에 액세스 할 수 있습니다. user 개체가 필요한 코드가 then 처리기 내부에서 발생하는지 확인하십시오.

this.store.find('userProfile', 1).then(function(user) { 
    order.set('user', user); 
    order.save(); 
}); 
1

코드는 화면에 표시된 순서대로 실행되지 않기 때문에 발생합니다. this.store.find() 의미 : 검색 결과에 올 때 시작해야 백그라운드 스레드를 시작

그래서 기본적으로, 당신은 병렬로 일이 몇 가지를 시작하고 당신은 find() 또는 save() 먼저 실행 여부를 알 수 없습니다.. console.log()save()에 추가하여 실제로는 코드 앞에 find() (적어도 가끔씩)이 실행되는 것을 볼 수 있습니다.

이 솔루션은 find()에 대한 콜백의 내부 save()을 이동하는 것입니다 :

this.store.find('userProfile', 1).then(function(user) { 
    console.log(user.get('id')); 
    order.set('user', user); 

    order.save().then(function() { 
    _this.transitionTo('products.index'); 
    }); 
}); 

관련 :

관련 문제