Javascript function returns undefined in other function -
i have function , want call in other function api key. if return value undefined.
how can solve this?
function getapikey(callback) { var db = app.db; db.transaction( function (tx) { tx.executesql("select api_key settings id='1'", [], function (tx, result) { var apikey = result.rows.item(0).api_key; alert(apikey); // here works return apikey; }); } ); } function getdata() { var mykey = getapikey(); alert(mykey); // undefined }
you have callback
being passed param, use it! can't return
async calls!
function getapikey(callback) { var db = app.db; db.transaction(function (tx) { tx.executesql("select api_key settings id='1'", [], function (tx, result) { var apikey = result.rows.item(0).api_key; callback(apikey); }); }); } function getdata() { getapikey(function(key) { var mykey = key; /* logic mykey should done in block */ }); }
Comments
Post a Comment