如何在JavaScript中定义变量?

时间:2015-11-14 15:54:38

标签: javascript ajax service-worker

这只是一个愚蠢而简单的问题。我只需要进一步的响应数据......但那是未定义的..

  

未捕获的ReferenceError:未定义url

self.addEventListener('push', function(event) {
  console.log('Push message', event);

  // perform action
  fetch('slack-url')  
    .then(  
      function(response) {  
        if (response.status !== 200) {  
          console.log(response.status);  
          return;  
        }

        // Examine the text in the response
        response.json().then(function(data) {  
          console.log(data);
          var url = data;
        });  
      }  
    )  
    .catch(function(err) {  
      console.log('Fetch Error :-S', err);  
    });


  var title = 'Push message';
  var stack_url = url;

  event.waitUntil(
    self.registration.showNotification(title, {
      stack_url:stack_url
    })
  );
});

1 个答案:

答案 0 :(得分:0)

self.addEventListener('push', function(event) {
  console.log('Push message', event);
  var url = "";
  // perform action
  fetch('slack-url')  
    .then(  
      function(response) {  
        if (response.status !== 200) {  
          console.log(response.status);  
          return;  
        }

        // Examine the text in the response
        response.json().then(function(data) {  
          console.log(data);
          url = data;
        });  
      }  
    )  
    .catch(function(err) {  
      console.log('Fetch Error :-S', err);  
    });


  var title = 'Push message';
  var stack_url = url;

  event.waitUntil(
    self.registration.showNotification(title, {
      stack_url:stack_url
    })
  );
});