2014-04-19 3 views
2

다음 코드가 있습니다. 나는 비교적 새로운 nodejs이다. & jsMysql 데이터베이스가있는 Nodej 반환 값

1. 로그에서 값을 가져오고 싶지만 정의되지 않는다. 2. 로그에만 로그가 출력됩니다.

나는 nodeJS return value from callbackhttps://github.com/felixge/node-mysql을 읽었지만 반환 값에 대한 예는 없습니다.

node-mysql 페이지에서 주어진 예제와 함께 return 문을 사용하는 방법을 모르겠다.

답변

4

비동기식이므로 콜백을 전달하여 준비가되었을 때 값을 가져와야합니다. 예 :

exports.location_internal = function(req, res, next) { 
    getExternalLocation(2, function(err, rows) { 
    if (err) 
     return next(err); 
    console.log(rows); 
    res.send(rows); 
    }); 
}; 

function getExternalLocation(id, cb) { 
    pool.getConnection(function(err, conn) { 
    if (err) 
     return cb(err); 

    conn.query("select * from external_geo_units where geo_unit_id = ?", 
       [id], 
       function(err, rows) { 
     conn.release(); 
     cb(err, rows); 
    }); 
    }); 
} 
+1

우수함! 감사 –

관련 문제