2016-08-09 1 views
0

내 프로젝트에서 원격으로 API를 점검해야하고 한 번하고 싶습니다. 원하지 않는 것을 리턴하면 다른 시간을 확인해야합니다 그러나 5 초 후에 10, 15, 30 초 후에 지연됩니다. 30 초 후에도 여전히 API에서 예상 한 응답이 없다면 클라이언트에 오류가 발생하지만 API가 예상 한 응답으로 응답하면 (# 1 또는 # 2 또는 # 3 또는 # 4에서) 원하는 결과가 반환됩니다. 수표를 그만 둔다. Meteor.setTimeout을 사용하여 서버 메소드에서 리턴 할 수 없음

내 방법 :

Meteor.methods({ 
    'verifyUserOwnership': function() { 
     function checkCard() { 
      return false; // for testing, simulating the bad answer from api 
     } 

     var checkOwnership = function(nbrTry, delay){ 
      if (checkCard()) { 
       //I will do something if API responds with wwhat I want 
      } else { 
       if(nbrTry < 3){ 
        delay +=5000; 
        Meteor.setTimeout(function(){checkOwnership(nbrTry+1, delay);}, delay); 
       } else { 
        throw new Meteor.Error('Card could not be found');// has to be sent to client if after all the tries, API did respond with somwthing bad 
       } 
      } 
     } 
     checkOwnership(0, 0); 
    } 
}); 

그러나이 코드와 함께이 오류가 : 나는 섬유와 Meteor.bindEnvironment에 대해 뭔가 생각하지만, 내가 할 방법을 잘 모르겠습니다

I20160809-12:56:06.928(-4)? Exception in setTimeout callback: Error:  [Card could not be found] 
I20160809-12:56:06.929(-4)?  at checkOwnership  (server/methods.js:74:17) 
I20160809-12:56:06.929(-4)?  at server/methods.js:72:40 
I20160809-12:56:06.929(-4)?  at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1) 
I20160809-12:56:06.929(-4)?  at packages/meteor/timers.js:6:1 
I20160809-12:56:06.930(-4)?  at runWithEnvironment (packages/meteor/dynamics_nodejs.js:110:1) 

을 그것.

감사합니다.

+0

코드를 수정할 수 있습니까? 내가보기에는 혼란 스럽네요. 'function checkOwnership()'은 서버 측 메소드 코드의 일부입니까? –

+0

** checkOwnership() **은 메소드 내부의 함수입니다. 이 함수는 4 번 실행되고 지연이 추가 될 때마다 네 번째 시간에 ** checkCard() ** 함수가 false를 반환하면 (내 예제에서는 false를 강제로) false로 메쏘드를 던지려고합니다. .Error와 if ** checkCard() **가 true를 반환하면 메소드가 클라이언트에게 "VALID"를 반환하도록합니다. (callback의 meteor.call에서. – NOaMTL

+0

참조 http://stackoverflow.com/a/25941287/2805154 당신의 질문에 대한 가능성있는 대답, 특히 방법에서 여러 비동기 요청을 조정하려고 시도하는 경우. –

답변

0

노드 파이버를 사용하여 순환 콜백이 값을 반환 할 때까지 중지/제한합니다. 그래서 당신은 차단을 모방합니다. 유성 NPM은 다음과 같이 코드를 변경 --save 섬유

를 설치 :

if (Meteor.isServer) { 
var Future = Npm.require('fibers/future'); 


Meteor.methods({ 
    'verifyUserOwnership': function() { 
     var fut = new Future(); 
     function checkCard() { 
      return false; // for testing, simulating the bad answer from api 
     } 

     var checkOwnership = function(nbrTry, delay){ 
      if (checkCard()) { 
       //I will do something if API responds with wwhat I want 
      } else { 
       if(nbrTry < 3){ 
        delay +=5000; 
        nbrTry +=1; 
        console.log(nbrTry) 
        console.log("out") 
        Meteor.setTimeout(function(){ 
         console.log("in") 
         checkOwnership(nbrTry, delay); 
         fut.return('done'); 
         return 'done'; 
        }, delay); 
       } else { 
        console.log('err'); 
        throw new Meteor.Error('Card could not be found');// has to be sent to client if after all the tries, API did respond with somwthing bad 
       } 
      } 
     } 
     if (nbrTry = 0) 
      console.log("call") 
      checkOwnership(0,0); 
    } 
}); 
} 

과 코드가 이제 서버 측을 실행하는 것을 알 수 귀하의 유성 응용 프로그램 디렉토리에 우선

설치. 체크 로그 서버 쪽!

이 코드는 오류를 반환하지 : 오류 : 미래는 당신이 당신의 조건에 따라 원하는 것입니다 번 이상

하지만 최종 오류가 체인에 반환 해결 : [카드를 찾을 수 없습니다]

또한 this link

관련 문제