This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
I can't set value inside function. When I try to alert this value, I'm getting: undefined. How to take this value outside?
var startTrasyLat;
L.esri.Geocoding.geocode()
.text(document.getElementById('start').value)
.run(function (err, results, response) {
if (err) {
console.log(err);
return;
}
startTrasyLat = results['results'][0].latlng.lat;
});
alert(startTrasyLat);
Looks like you are trying to call an asynchronous function. You should put the alert inside said function, like this:
var startTrasyLat;
L.esri.Geocoding.geocode().text(document.getElementById('start').value).run(function (err, results, response) {
if (err) {
console.log(err);
return;
}
startTrasyLat = results['results'][0].latlng.lat;
alert(startTrasyLat);
});
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
I have a function that needs to get data from another function, but the second one is returning undefined and i don't know why.
function firstOne() {
const companys = companysList();
console.log(companys); // LOG UNDEFINED
};
.
function companysList(){
db.query('SELECT id FROM companys WHERE id > 0', (error, result) =>{
if (error) throw error;
console.log(JSON.stringify(result)); // LOG THE ids!
return result;
});
};
your companysList function doesn't return anything
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
I have a problem with function check_mod.
function check_mod(user_id) {
db.each(`SELECT mode FROM users WHERE id = ` + user_id, [], (err, row) => {
if (err) {
throw err;
}
if(row.mode == 1)
{
return true;
}
});
}
if(check_mod('286927644405137408')) console.log("okay");
When i try to use this function, it returns "undefined" and i don't know why. I think that i should use async and await but i don't know how to use it.
I'm not sure but, I do notice that there is no semicolon at the end of the sql statement which could be causing the issue.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 5 years ago.
Although I found similar questions to mine, I couldn't solve the problem on my own.
In my '../models/user' model I want to find all users and put them into array, and return that array to the controller(where I will use the info).
Here is my code:
var mongoDatabase = require('../db');
var database = mongoDatabase.getDb();
function find() {
var test;
database.collection("customers").find().toArray( function(err, docs) {
if(err) throw err;
console.log(docs); //works fine
//I'd like to return docs array to the caller
test = docs;
});
console.log(test); //test is undefined
}
module.exports = {
find
};
I also noticed, that 'console.log(test)' goes before 'console.log(docs)'. I tried passing 'docs' argument as function parameter to 'find', but without result.
The best way is to use Promises. Do it like this.
function getUsers () {
return new Promise(function(resolve, reject) {
database.collection("customers").find().toArray( function(err, docs) {
if (err) {
// Reject the Promise with an error
return reject(err)
}
// Resolve (or fulfill) the promise with data
return resolve(docs)
})
})
}
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
How to make a function wait until a callback has been called using node.js
(11 answers)
Closed 5 years ago.
I'm learning node.js and I have a problem. How to get data from function to variable?
function loadOrInitializeTaskArray(file, cb) {
fs.exists(file, function(exists) {
var tasks = [];
if (exists) {
fs.readFile(file, 'utf8', function(err, data) {
if (err) throw err;
var data = data.toString();
var tasks = JSON.parse(data || '[]');
cb(tasks);
})
} else {
cb([]);
}
})
}
function listTasks(file) {
loadOrInitializeTaskArray(file, function(tasks) {
for (var i in tasks) {
console.log(tasks[i]);
}
})
}
Function listTasks works correctly, but I would like create own function, for example customListTasks:
function customListTasks(file) {
var list = loadOrInitializeTaskArray(file, function(){});
console.log(list);
}
This not return me errors, but console.log on list return me "undefined". How can I get this data to variable list?
Short answer: you can not.
Because the loading in loadOrInitializeTaskArray is asynchronous, all the magic has to happen in the callback that you are passing. You cannot just 'return' a value from it.
Option 1: Place all logic that depends on the loaded data in the anonymous function that you pass as callback.
var list = loadOrInitializeTaskArray(file, function(tasks){
console.log(tasks);
});
Option 2: Use Promises. They work essentially like callbacks, but are slightly more flexible and make chaining easier.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
Heres my code. I basicly want to return the 'body' var in my getFollows function. Setting vars obviously dont work, no idea how to get the variable. I can't change getUserFollowedChannels because its a package, and I need to return it to the function directly because of meteor server->client stuff.
'twitch.getFollows'() {
var followers = twitch.getUserFollowedChannels('atlatonin', function(err, body) {
if (err) {
return err;
} else {
console.log(body.follows[0].channel.display_name);
return body.follows[0].channel.display_name;
}
});
return followers;
},
You can pass a callback function as below:
'twitch.getFollows': function(done) {
twitch.getUserFollowedChannels('atlatonin', done);
}
And invoke the function as below:
twitch.getFollows(function(err, body) {
if (err) {
console.log(err);
//return err;
} else {
console.log(body.follows[0].channel.display_name);
//return body.follows[0].channel.display_name;
}
});