2014-05-12 6 views
0

이것은 일반적인 JS 질문이며 edge.js에만 해당되는 질문이 아닙니다. edge.js를 통해 SQL Server 액세스를 사용하고 싶었습니다. 어떻게 logResult보다는 그 함수 내부의 데이터를 사용하여 결과를 반환 할 수MSSQL에 액세스하는 edge.js func에서 데이터를 반환하는 방법은 무엇입니까?

var edge = require('edge'); 
var _ = require('lodash'); 


var thingsAccess = function() {/* 
    select * from Things where Status = 'A' and [email protected] 
*/}; 

var getThingsList = edge.func('sql', thingsAccess); 

var logResult = function (error, result) { 
    if (error) throw error; 

    _.each(result, function (thing) { 
     console.log(thing.Id, thing.Category, thing.Name); 
    }); 
}; 

var thingsOfType = function (type) { 
    return function() { 
     getThingsList({ thingType: type }, logResult); 
    }; 
}; 


var xThings = thingsOfType('X'); 
var yThings = thingsOfType('Y'); 


xThings(); 
yThings(); 

내 질문은 :

var edge = require('edge'); 

var getTop10Products = edge.func('sql', function() {/* 
    select top 10 * from Products 
*/}); 

getTop10Products(null, function (error, result) { 
    if (error) throw error; 
    console.log(result); 
    console.log(result[0].ProductName); 
    console.log(result[1].ReorderLevel); 
}); 

the following snippet from its GitHub site을 바탕으로 나는 내 자신의 데이터베이스에 액세스하려면 다음을 썼다? 콘솔에 이제 로그하는 _.each(result, ...) 구조 대신, 나는 Thing 개체의 배열을 반환하는 return _.map(result, ...)과 같은 것을 사용하고 싶습니다. 그러나이 함수는 getThingsList에 대한 콜백이므로 결과를 넣을 곳이 없습니다.

코드를 어떻게 구성했는지 알아 내 유일한 옵션은 배열을 외부 범위에 선언하고 내부에서 밀어 넣는 것입니다. logResult. 내가 잘못하고 있다고 확신하지만 구조 조정 방법을 알 수는 없다. 어떤 제안?

답변

2

"반환"할 수 없으며 로그 기능과 마찬가지로 콜백에서 작업해야합니다.

var thingsOfType = function (type, onSuccess, onError) { 
    getThingsList({ thingType: type }, function (error, result) { 

     // Process if no error 
     if (!error) { 
      if (onSuccess instanceof Function) { 
       onSuccess(result); 
      } 
      // silently do nothing if onSuccess is not a function 
      return; 
     } 

     // Handle errors using the onError callback or throw 
     if (onError instanceof Function) { 
      onError(error, result); 
      return; 
     } 

     // unhandled errors 
     throw error; 

    }); 
}; 

이제 배열 또는 각 내에서 사용할 수 있습니다 :

thingsOfType("X", function (result) { 

    // Here result should be an Array 
    var len = result.length; 

    // You can use result here with or without _.each 

    // Do something here 

} /*, not an onError function defined */); 

사용 예제는 방법과 이벤트 기반의 코드를 더 이동 :

var printAllThingsOf = function (type, onSuccess, onError) { 

    thingsOfType(type, function (result) { 

     // Print each one 
     _.each(result, function (thing) { 
      console.log(thing.Id, thing.Category, thing.Name); 
     }); 

     // this function is the thingsOfType-onSuccess function, 
     // so propagate the new printAllThingsOf-onSuccess function; 
     // "call the callback" 
     if (onSuccess instanceof Function) { 
      onSuccess(result); 
     } 

    }, onError /* propagate the onError */); 

}; 

printAllThingsOf("X"); // just do it or fail. onSuccess and onError are undefined. 
+0

감사 @metadings을 A에 대한 명확하고 포괄적 인 대답. printAllThingsOf의 정의 끝에서 빗나간 오른쪽 괄호를 제거했습니다. –