【问题标题】:function not called in promise未在承诺中调用的函数
【发布时间】:2016-03-07 05:03:26
【问题描述】:

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

在节点中,我正在创建几个 Firebase“观察者”来处理我的 Firebase 中的数据的一些清理工作,并在参数状态发生变化时发送 SMS。在下面的代码中,我有一个 startChildChangedWatcher 和一个 sendSMS 函数。当我尝试从bitly.shorten() 的承诺中调用sendSMS 时,就会出现我的问题。它永远不会被执行,尽管在 Promise 之外调用时它可以正常工作。我尝试定义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);
    }
  });
};

【问题讨论】:

  • sendSMS 函数是否被执行??
  • @himangshuj 不,抱歉忘了把它放在那里。它没有。
  • 你混合了承诺和回调。你不会过得很好。你可以尝试承诺authWithCustomToken吗?
  • 在调用 sendSMS =) 时,您可能希望使用 shortUrl 而不是 url
  • @Droogans 承诺它有效!谢谢!

标签: javascript firebase promise


【解决方案1】:

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

【讨论】:

  • 抱歉,这不是问题,而是“复制粘贴”错误进入 SO
猜你喜欢
  • 2017-08-12
  • 2016-12-19
  • 2019-12-31
  • 2017-03-26
  • 1970-01-01
  • 2022-01-08
  • 2018-07-06
  • 2016-03-21
  • 2019-06-07
相关资源
最近更新 更多