函数未在 promise 中调用
function not called in promise
我一直在寻找答案,因为我确信以前一定有人问过,但我似乎找不到一个对我来说有意义的答案。
在节点中,我正在创建几个Firebase"观察程序",用于处理Firebase中数据的一些清理,并在参数状态更改时发送短信。在下面的代码中,我有一个startChildChangedWatcher
和一个sendSMS
函数。当我尝试从bitly.shorten()
的承诺中调用sendSMS
时,我的问题发生了。它永远不会被执行,尽管在承诺之外调用时它可以正常工作。我尝试定义var self = this;
因为我认为它与范围有关,然后调用self.sendSMS("")
,但没有运气。此外,只调用 promise 中的两个 log 语句中的一个。我相信这很简单,但有时在寻找类似的答案时很难知道到底要搜索什么。
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 to 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 to 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);
}
});
};
我认为您没有在代码中定义url。
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 to 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 to 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);
}
});
};
相关文章:
- 函数在promise被解析后被调用,但Jasmine未通过测试.为什么?
- AngularJS和promise值在调用本地函数时的效果-未定义
- 多次调用方法后返回相同promise的模式
- 我如何防止错误“;未捕获(在promise中)DOMException:play()请求被对pause()的调用中断&”
- 多次调用promise函数,直到另一个promise函数满足条件
- bluebird promise catch() 没有使用 Promise.CancelError 调用内部函数
- 使用 Promise 在另一个函数中调用带有 Bluebird 承诺库的函数
- JavaScript Promise 解析并拒绝函数调用序列
- 使用promise或setTimeout确定延迟函数的调用顺序
- 使用Node中的Q promise库对值数组顺序调用/执行相同的函数;并返回带有结果的新数组/集合
- Javascript Promise().然后防止在执行第一个调用之前重新调用该函数
- 当使用promise时,为什么最后一个会被调用
- 在循环中调用Bluebird Promise函数
- 带有拒绝调用和抛出错误的Promise构造函数
- 如何在循环中调用Promise函数并保存其返回值
- 然后,在前一个完成之前调用 Promise
- 在另一个函数中调用promise函数时出错
- 在for循环结束时调用promise
- 链接2个异步调用(promise API)以串行运行
- ' fetch ', work with mode ' no-cors '不会调用promise.然后回调