2017-11-15 1 views
2

로그인 페이지의 nodejs를 사용하여 데이터베이스 arangodb에서 간단한 쿼리를 실행하고 싶습니다.arangodb 및 nodejs를 사용한 간단한 쿼리

var arango = require('arangojs'); 
/*---*/ 
Database = arango.Database; 
db = new Database('http://127.0.0.1:8529'); 
db.useDatabase('MyDB'); 
db.useBasicAuth('root', 'root'); 
/*---*/ 

//Post Methods 
var username; 
var passworddb; 

//Login method 
app.post('/', function (request, response) { 

    username = request.body.user.name; 

    db.query('FOR u IN User FILTER u.firstname == "'+username+'" RETURN u.password').then(
     cursor => cursor.all() 
    ).then(
     key => console.log('key:', key), 
     //key => passworddb = key, 
     err => console.error('Failed to execute query') 
    ); 

    console.log(passworddb) 
}); 

그것은 일하고있어,하지만 난 키의 결과 passworddb 내 VAR에 저장할 수 없습니다 ... 내가 undertand에없는 ... 요청이 arangodb

콘솔에서 비동기입니다. write (passworddb)는 "undefined"를 반환합니다. 및 console.log ('key :', key) 다음에 올바르게 키를 반환합니다.

내 질문은 : 어떻게하면 arangodb (한 번 요청에 대한 쿼리)에서 암호를 얻을 수 내 var에 값을 저장하는 방법?

답장을 보내 주셔서 감사합니다.

답변

0

console.log(passworddb)은 비동기 요청으로 인해 //key => passworddb = key이 발생하기 전에 실행됩니다. 따라서 암호를 기록하려고 할 때 db 응답은 아직 도착하지 않았습니다. 암호는 passworddb에 정확하게 저장됩니다. console.log는 너무 빨리 저장됩니다. 수행 할 수 있음을 확인하기 위해

key => { 
    passworddb = key; 
    console.log(passworddb); 
} 

을 확인할 수 있습니다.

+0

작동합니다! 고맙습니다 ! – Erako