How to take the value of one function within another

Asked

Viewed 562 times

0

Guys I’m not getting the value of "var stop" to use elsewhere just appears on the console. Can someone help me return please?!

Thank you!!

game = require('./../model/Game.model')()// Aqui busco meu model de Schema mongooseJS.
modules = require('./../model/modules'); // Aqui estou chamando a minha pagina de funções (modulos).
var result = modules.getResult(game); //Aqui estou buscando o resultado na ma minha função a qual tenho dúvida.

-----------------------Page of models---------------------------------------------

var Models = function(){

    this.getResult = function(game){
        //game.findOne uso para pesquisar no meu banco de dados o game que está em progresso. Essa ferramenta é do mongooseJS pois meu bd é todo em JSON Ex. gamesDB{gameId:116, gameStatus:"in progress", gameResult: 100.99;}
        game.findOne({gameStatus:'in progress'}, function(err, gamesDB){
            if(err){throw err}
            var stop = gamesDB.gameResult; //Aqui ele traz para mim o resultado de meu banco de dados 100.98
            //console.log(stop); // Aqui ele retorna no console perfeitamente
            return stop; // Quero usar esse valor e não consigo, fica como indefinido =(
        });
     }
   //se a var stop estivesse aqui funcionaria de boa
}
module.exports = new Models();
  • 1

    game.findOne is probably asynchronous. Where you want to use this variable stop? in another capacity?

  • That’s right Sergio, but this function is in another part of my module page. I will just call her to present this result Ex: test = modules.sumTotalBets(game); Here would call the function’s Return result

  • You must call it within that function. As in this case: https://answall.com/a/79960/129

  • This error appears: Typeerror: callback is not a Function. I did some callback tests here but only in this function does not work, I must be doing something wrong

  • I’m not saying you have to copy the code from there, but the problem is the same. Where do you have it now return stop; you must have aTuaFuncao(stop);, and so call this function that needs the value of stop. makes sense?

  • I made like this in this picture but I don’t know where to call link

  • Can you make a flow diagram of that code? is, when you call game.findOne what happens/what does this function and what function do you call next? In the image above you have a function teste that you don’t have here in question... or else show more code to understand the flow.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.