javascript - function not called in promise -
i've been looking on answer this, sure must have been asked before, can't seem find 1 makes sense me.
in node i'm creating couple of firebase 'watchers' handles sanitization of data in firebase , send sms when status of parameter changes. in below code i've got startchildchangedwatcher
, sendsms
function. problem occurs when try call sendsms
within promise in bitly.shorten()
. never gets executed, although works fine when called outside of promise. i've tried defining var self = this;
because thought had scope, , calling self.sendsms("")
, no luck. also, 1 of 2 log statements inside promise gets called. i'm sure pretty simple, it's difficult know search when looking similar answers.
ref.authwithcustomtoken(token, function(error, authdata){ if (error) { console.log(error); } else { console.log("login successful"); startchildchangedwatcher(); ... } }); var startchildchangedwatcher = function() { deliveriesref.on('child_changed', function(snapshot) { console.log(snapshot.val()); if (snapshot.val().completed === true) { if (snapshot.val().urltoshorten !== undefined) { console.log("image url found, proceeding shorten url"); bitly.shorten(snapshot.val().urltoshorten) .then(function(response) { var shorturl = response.data.url; //this prints fine console.log('url received, sending sms'); sendsms("your url is: " + shorturl); //this never prints console.log("url shortened, proceeding send sms"); }, function(err){ console.log(err) }); } else { ... } } }); }; var sendsms = function(message) { console.log(message); twilio.sendmessage({ to: +447564908855, from: +441241797052, body: message }, function(err, responsedata) { if (err) { console.log("error sending message: " + err); } else { console.log("message sent. response: " + responsedata); } }); };
i don't think have defined url in code.
sendsms("your url is: " + url);
sendsms("your url is: " + shorturl);
ref.authwithcustomtoken(token, function(error, authdata){ if (error) { console.log(error); } else { console.log("login successful"); startchildchangedwatcher(); ... } }); var startchildchangedwatcher = function() { deliveriesref.on('child_changed', function(snapshot) { console.log(snapshot.val()); if (snapshot.val().completed === true) { if (snapshot.val().urltoshorten !== undefined) { console.log("image url found, proceeding shorten url"); bitly.shorten(snapshot.val().urltoshorten) .then(function(response) { var shorturl = response.data.url; //this prints fine console.log('url received, sending sms'); sendsms("your url is: " + url); //this never prints console.log("url shortened, proceeding send sms"); }, function(err){ console.log(err) }); } else { ... } } }); }; var sendsms = function(message) { console.log(message); twilio.sendmessage({ to: +447564908855, from: +441241797052, body: message }, function(err, responsedata) { if (err) { console.log("error sending message: " + err); } else { console.log("message sent. response: " + responsedata); } }); };
Comments
Post a Comment