2014-11-29 1 views
0

내 userProfileController.js 결과에서 데이터를 얻을 수있는 것은내가 thinky.io에서 데이터를 뽑을 수 있지만 내가

var UserProfileSerice = require('../../Services/User/UserProfileService.js'); 

module.exports = function(app){ 

app.get('/tester' , ensureAuthenticated, function(req, res){ 


    var sonuc ; 

    UserProfileSerice.getUserByEmail(function(result){ 
     sonuc = result; 
    }) 

    console.log('sonuc' +sonuc); 

    res.render('UserPages/userPage'); 
}) 

}

이 기능이 UserProfileSerice 안에 정의되지 않습니다. js 파일 콜백이 작동하지 않습니다.

var UserModel = require('../../Models/User/UserModel.js'); 
var thinktConfig = require('../../Utils/rethinkdb/config.js'); 
var thinky = require('thinky')(thinktConfig.rethinkdb); 
module.exports ={ 



getUserByEmail : function (callback) { 

    UserModel.filter({email: "[email protected]"}).run().then(function(result) { 
     callback(result); 

    }); 

}} 

답변

1

전화가 비동기이기 때문에 간단합니다. 콜백이 정의되었지만 콜백이 반환되기 전에 실행이 console.log (myResult)를 실행하려고합니다.

var myResult; 
UserModel.filter({username : 'selim'}).run().then(function(result){ 
    myResult = result; 
    console.log("inside callback", myResult); // write result 
}); 
console.log("outside callback", myResult); // do not write 

콘솔에서이 표시됩니다 :이하여 코드를 대체하면 나는 내기

"outside callback" undefined 
"inside callback" myResultValue 

그러나, 당신이 원하는 것은 그런 다음, 다른 곳에서 내 결과 값을 재사용하는 경우 실행이 완료된 후에 다른 메소드를 콜백 할 필요가있다. 당신은 당신이 뭘 하려는지에 대해 좀 더 컨텍스트를 제공 할 수 있습니다, 당신은

+0

otherMethod에서 올바른 결과를 확인한다이 방법을

var otherMethod = function(data){ console.log(data); }; var myResult; UserModel.filter({username : 'selim'}).run().then(function(result){ myResult = result; console.log("inside callback", myResult); // write result otherMethod(myResult); }); 

: 당신은 뭔가를해야 할 것인가? 그 노드가 완전히 비동기 적이기 때문에, 당신이하고있는 것처럼 콜백 바깥에서 값을 재사용하는 일은 드물다. – QuantumLicht

+0

예 otherMethod가 작동 중입니다. 감사합니다 @ QuantumLicht,하지만 변수에 내 결과를 할당 할 수 없습니다 – slmkrnz

+0

@slmkrnz 무엇을하려고하는지 더 자세히 알려주십시오. 나는 너를 다르게 도울 수 없다. – QuantumLicht

관련 문제