Return from callback function in Javascript

前端 未结 4 593
时光说笑
时光说笑 2021-01-27 06:21

How do I get the return value from inside a value of node.js/javascript callback?

function get_logs(){
    User_Log.findOne({userId:req.user._id}, function(err,          


        
4条回答
  •  我在风中等你
    2021-01-27 06:31

    You can't. You should instead pass another callback to your function. Something like this:

    function get_logs(callback){
        User_Log.findOne({userId:req.user._id}, function(err, userlogs){
            if(err) throw err;
            if(userlogs){
                callback("hello there is a logs");
            } else {
                callback("there is no logs yet...");
            }
        })
    }
    
    get_logs(function(arg1) {
       console.log(arg1);
    });
    

提交回复
热议问题