2017-12-01 9 views
0

에서 데이터를 얻을하는 방법.자바 스크립트 (NodeJS) : 나는이 사건의 JSON 배열을 반환 할 listEvents을 변경하려면 <a href="https://developers.google.com/google-apps/calendar/quickstart/nodejs" rel="nofollow noreferrer">https://developers.google.com/google-apps/calendar/quickstart/nodejs</a></p> <p>: 다시이 예에 대해서는 Google 캘린더 API 콜백

현재이 같은라고 : 다음이

function authorize(credentials, callback) { 

내가 ListEvents에 return 문을 추가 시도 : "listEvents는"콜백에 전달하는 기능이

authorize(JSON.parse(content), listEvents); 

입니다

위의 console.logs가 console.log 이전에 나오기 때문에 나에게 비동기가 발생한다는 것을 알고 있습니다. listEvents 함수의 출력. 나는 또한 "기다리는"단어를 던지려고했지만 행운이 없었다.

내가 listEvents에서의 추가적인 파라미터를 설정 시도는 :

var jsonEvents; 
authorize(JSON.parse(content), listEvents(jsonEvents)); 
console.log("Json Events="); 
console.log(jsonEvents); 

로 이어지는 "오류 : 콜백 함수가 아니다."

업데이트 : @Tuches 응답을 기반으로 작동하도록했습니다. 지금까지 확장해야하는지 알고 싶습니다.

authorize(JSON.parse(content), function(token) { 
     console.log("Got Token"); 
     //console.log(token); 
     listEvents(token, function(jsonResult) { 
      console.log("Json Callback Events="); 
      console.log(jsonResult); 
     }); 
    }); 

답변

1

google 캘린더 API에 대해 모르겠지만 두 번째 인수를 빨리 읽는 것은 콜백 함수입니다. 즉, 권한 부여 결과가 끝나고 listEvent가 호출됩니다. 그래서 실제로 당신이 무엇을해야 핸들러 중 하나는 listEvents 안쪽이 데이터 반환 또는 당신처럼 콜백을 반환 listEvents을 수정할 수 있습니다

function listEvents(auth, callback) { 
    // ... implementation of the function 
    // When the function is done an there's data to 
    // return, callback the data 
    callback(data); 
} 

이 같은 작업을 수행하여 listEvents에서 데이터를 반환 처리 할 수있는 것이 방법 :

authorize(JSON.parse(content), listEvents(data, function(response) { 
     console.log(response); // <-- return from listEvents  
    }) 
); 

편집 : 코드에 작은 수정. = 150 REQ :

+0

가 좋아, 그 의미가 있습니다,하지만 난 API 라이브러리 중 하나에이 오류가 발생합니다 : λ 노드 testGoogleCalendarAPI.js E : \ GitHub의 \ NealWalters \ RabbiJoseph \ node_modules \ googleapis \ LIB \ apirequest.js authClient.request (options, callback); ^ TypeError : authClient.request가 함수가 아닙니다. – NealWalters

+0

authorize에서 토큰을 다시 가져온 다음 listEvents를 동기화 방식으로 호출 할 수 있지만 listEvents는 calendar.list.events에서 다시 콜백을하기 때문에 더 복잡합니다. 함수 (오류, 응답). – NealWalters

+0

내 업데이트보기, 작동하지만 어쩌면 최선의 방법은 아닙니다. – NealWalters