可变范围; var undefined或相同的var循环

时间:2016-05-05 22:09:07

标签: javascript

Meteor.methods({
      'list.messages' () {
        getTwilioMessages(function(err, data) {
          if (err) {
            console.warn("There was an error getting data from twilio", err);
            return
          }
          data.messages.forEach(function(message) {
            if (SMS.find({
                sid: message.sid
              }).count() > 0) {
              return;
            }
            var image;
            if (message.numMedia > 0) {
              var images = [];
              Meteor.wrapAsync(client.messages(message.sid).media.list(function(err, data) {
                console.log(data)
                if (err) throw err;
                Meteor.wrapAsync(data.media_list.forEach(function(media, index) {
                  mediaImage = (function() {
                    let mediaImg = 'http://api.twilio.com/2010-04-01/Accounts/' + media.account_sid + '/Messages/' + media.parent_sid + '/Media/' + media.sid;
                    return mediaImg
                  });
                }));
                images.push(mediaImage());
                console.log("Just One:" + mediaImage())
                console.log("IMAGESSSS " + mediaImage())
              }));
              message.image = mediaImage();
              console.log("test" + image)
            } else {
              message.image = null
            }
            if (message.from === Meteor.settings.TWILIO.FROM) {
              message.type = "outgoing";
            } else {
              message.type = "incoming";
            }
            SMS.insert(message)
          });
        });
        getTwilioMessages();
        Meteor.setInterval(getTwilioMessages, 60000);
        //   client.calls.get(function(err, response) {
        //     response.calls.forEach(function(call) {
        //         console.log('Received call from: ' + call.from);
        //         console.log('Call duration (in seconds): ' + call.duration);
        //     });
        // });
      },

我希望在data.media_list.forEach中看到不同的图片网址。但是,它返回相同的图像。在控制台中,我得到了理想的结果。

我想要这些结果:

message.image = 'http://api.twilio.com/2010-04-01/Accounts/3039030393/Messages/303030/media/3kddkdd'
message.image = 'http://api.twilio.com/2010-04-01/Accounts/3039sdfa393/Messages/303030/media/3asdfdfsa' inserted into the correct message. 
  

相反,我反复得到相同的URl,导致相同的图像   我所有的消息。我猜我的范围不正确,但我有   试图关注:2ality guide on scopingExplaining JavaScript Scope And Closures没有运气。

0 个答案:

没有答案