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();
game.findOne
is probably asynchronous. Where you want to use this variablestop
? in another capacity?– Sergio
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
– Rodrigo Araujo
You must call it within that function. As in this case: https://answall.com/a/79960/129
– Sergio
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
– Rodrigo Araujo
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 haveaTuaFuncao(stop);
, and so call this function that needs the value ofstop
. makes sense?– Sergio
I made like this in this picture but I don’t know where to call link
– Rodrigo Araujo
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 functionteste
that you don’t have here in question... or else show more code to understand the flow.– Sergio
Let’s go continue this discussion in chat.
– Rodrigo Araujo