功能没有在诺言中调用

时间:2016-03-07 05:03:26

标签: javascript firebase promise

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

在节点中,我创建了几个Firebase“观察者”,用于处理Firebase中我的数据的一些清理,并在参数状态发生变化时发送短信。在下面的代码中,我有一个startChildChangedWatcher和一个sendSMS函数。当我尝试从sendSMS中的承诺中调用bitly.shorten()时出现问题。它永远不会被执行,虽然它在承诺之外调用时工作正常。我已经尝试定义var self = this;,因为我认为它与范围有关,然后调用self.sendSMS(""),但没有运气。此外,只会调用promise中的两个日志语句中的一个。我确信这很简单,但有时很难知道在寻找类似答案时究竟要搜索什么。

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);
    }
  });
};

1 个答案:

答案 0 :(得分:0)

我认为你没有在代码中定义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);
    }
  });
};