2014-10-21 2 views
0

Q 약속을 사용하여 내 몽구스 모델에서 비동기를 저장하려고합니다. Q.nfcall을 사용하거나 Q.defer()를 사용하여 연결하면 결과 객체의 동작이 달라집니다.Q.nfcall 및 Q.defer에서 mongoose mongodb model.save에 대한 예상치 못한 동작

특히 Q.defer()를 사용할 때 네이티브 콜백 함수를 사용하는 것과 마찬가지로 결과 객체의 모든 속성에 액세스합니다. 그러나 Q.nfcall을 사용할 때 모든 속성은 정의되지 않습니다. 더 많은 수수께끼, 두 개체를 모두 로깅, 전체 json 문자열을 볼 수 있습니다.

은 (커피 스크립트에서) 코드

Customer = new models.Customer({name: 'John Doe'}) 
console.log 'Customer.id' + Customer.id 

deferred = Q.defer() 
Customer.save((error, value) -> 
    deferred.resolve(value) 
) 

deferred.promise.then((customer) -> 
    console.log 'Deferred::customer.id: ' + customer.id 
    console.log 'Deferred::customer.name: ' + customer.name 
    console.log 'Deferred::customer: ' + customer) 

Q.nfcall(Customer.save.bind(Customer)).then((customer) -> 
    console.log 'Qnfcall::customer.id: ' + customer.id 
    console.log 'Qnfcall::customer.name: ' + customer.name 
    console.log 'Deferred::customer: ' + customer) 

이 출력

Qnfcall::customer.id: undefined 
Qnfcall::customer.name: undefined 
Qnfcall::customer: { __v: 0, name: 'John Doe', _id: 54461d5523867cc087ef4374 },0 
Deferred::customer.id: 54461d5523867cc087ef4374 
Deferred::customer.name: John Doe 
Deferred::customer: { __v: 0, name: 'John Doe', _id: 54461d5523867cc087ef4374 } 

내가 잘못 여기서 뭐하는 거지 볼 수 없습니다에게입니다. 어떤 도움을 크게 주셔서 감사합니다! 오류 저장된 객체와 갱신 된 행의 수 : 다음 Mongoose docs에서

답변

0

, 그것은 Model#save의 콜백이 세 개의 매개 변수가 나타납니다 콜백은 세 개의 매개 변수를 받게됩니다

를, ERR 오류가 발생하는 경우 그렇지 않으면 서 발견하고 데이터베이스에서 갱신 될 때 1이되며, 저장된 제품의 제품 및 numberAffected, 0

Q.nfcall 콜백에 제 파라미터를 검출하고, 배열로 약속 해결 [savedObject, numberAffected]을 입력하면 numberAffected을 잃지 않습니다. Q docs 참조하십시오 Node.js를 스타일 API가 다시 하나 이상의 오류가 아닌 매개 변수 (예 : child_process.execFile)를 호출하는 경우, Q는 이러한 패키지 것을

주를 약속의 성취 값으로 배열로를 수행 할 때 번역.

커피 스크립트에서이 배열을 압축 해제하는 destructuring 매개 변수를 사용할 수 있습니다

Q.nfcall(Customer.save.bind(Customer)).then ([customer, rowsAffected]) -> 
    console.log 'Qnfcall::customer.id: ' + customer.id 
    console.log 'Qnfcall::customer.name: ' + customer.name 
    console.log 'Qnfcall::customer: ' + customer 
    console.log 'Qnfcall::rowsAffected: ' + rowsAffected 
+0

감사를 응답하십시오. 당신 말이 맞아요. 저는 Q의 문서에서 그 부분을 놓쳤습니다. 이것은 그것을 해결! – user522082

관련 문제