函数未在 promise 中调用

function not called in promise

本文关键字:调用 promise 函数      更新时间:2023-09-26

我一直在寻找答案,因为我确信以前一定有人问过,但我似乎找不到一个对我来说有意义的答案。

在节点中,我正在创建几个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);
    }
  });
};