I am new to node.js and I wrote a code that invokes an API (GET) which returns a JSON array of documents. The GET works fine, the problem is that I can access the results inside the function but not from outside. The first alert inside the function display the JSON content fine, but I get an undefined value on the second alert.(result) which is outside the function. This is my piece of code:
function getNames(){
$.get("./api/myapi")
.done(function(data) {
if(data.length > 0) {
$('#databaseNames').html("Database contents: " + JSON.stringify(data));
var mylist2 = JSON.stringify(data);
alert (mylist2); // This alert is fine
return (mylist2);
}
});
}
var result = getNames() ;
alert (result); // Here I get "undefined".
via Leonardo Vidal
No comments:
Post a Comment