如果语句运行,Javascript循环未完成

时间:2018-06-12 21:52:01

标签: javascript node.js loops sails.js

我一直坚持这个问题一段时间了。我正在调用API - 获得结果就好了。我将值保存到数组。我遇到的问题是尝试从数组中获取特定值。我有一个for循环运行需要时间,所以当if语句运行时,循环没有达到该值。如果我使用Postman,我看到该值存在,它只是循环不及时执行。这是我的代码:

     var msg = {};
     var embed = {};
     var link = {};
     var msgIn = [];
     var rel = [];

     return SkypeService.getEvent(msg).then(function (result) {
            msg.eventsNext = result._links.next.href;           
            if (result && result.sender && result.sender.length > 0) {
                if (result.sender) {                                            
                    for (var item in result.sender) {
                        var event = result.sender[item].events;                        
                        for (var key in event) {                                
                            embed = event[key]._embedded;
                            msgIn.push(embed); 
                        }
                        for (var key in event) {
                            link = event[key].link;
                            rel.push(link);
                        }
                        // console.log(Object.entries(msgIn))                          
                        if(rel['rel'] == 'message') {
                            console.log("message is there")
                            if(msgIn.message) {
                                console.log("links exist")
                                if(msgIn.message.direction == "Incoming") {
                                    console.log("direction is there")
                                    msg.participant = msgIn.message._links.participant.href;
                                    msg.contMsg = msgIn.message._links.messaging.href;
                                    msg.msgIn = msgIn.message._links.plainMessage.href;
                                    break;
                                }
                            }
                        }

                        if(rel['rel'] == "messagingInvitation"){
                            console.log("invite there")
                            if(msgIn.messagingInvitation && msgIn.messagingInvitation.state !== "Failed") {
                                console.log("invite link") 
                                if(msgIn.messagingInvitation.direction == "incoming") {
                                    console.log("direction invite")
                                    msg.msgInviteState = msgIn.messagingInvitation._links.state;
                                    msg.acceptInvite = msgIn.messagingInvitation._links['accept'].href;
                                    msg.msgIn = msgIn.messagingInvitation._links.message.href;
                                    break;
                                }                                   
                            }
                        }
                        if(rel['rel'] == 'messaging') {
                            console.log('messaging there')
                            if(msgIn.messaging) {
                                if(msgIn.messaging.state == "Disconnected") {
                                    console.log("msgn Disconnected")
                                    msg.addMsg = msgIn.messaging._links.addMessaging.href;
                                    break;
                                }
                            }
                        }                                               
                    }
                }
            }
            console.log(msg)    
        })

另外,我附上了我的本地主机的屏幕截图,打印了msgIn,显示密钥存在。 msgIn

当我测试运行sails lift的代码时,我可以看到msgIn打印了几次,每个都增加了长度。这就是让我觉得if语句运行时for循环还没有完成的原因。

请帮助 - 我真的需要解决这个问题。我需要捕获链接,以便我可以在下一步中使用它们。

感谢。

1 个答案:

答案 0 :(得分:0)

我通过更改代码解决了我的问题。这是新版本:

     return 
     SkypeService.getEvent(msg).then(function 
     (result) {
            msg.eventsNext = result._links.next.href;

            if (result.sender) {                                            
                for (var item in result.sender) {
                    var event = result.sender[item].events;                        
                    for (var key in event) {                                
                        embed = event[key]._embedded;
                        link = event[key].link;                          
                    };

                    if(link['rel'] == 'message') {
                        console.log("message is there")
                        if(embed.message) {
                            console.log("links exist")
                            if(embed.message.direction == "Incoming") {
                                console.log("direction is there")
                                msg.participant = embed.message._links.participant.href;
                                msg.contMsg = embed.message._links.messaging.href;
                                msg.msgIn = embed.message._links.plainMessage.href;
                                break;
                            }
                        }
                    };

                    if(link['rel'] == "messagingInvitation"){
                        console.log("invite there")
                        if(embed.messagingInvitation) {
                            console.log("invite link") 
                            if(embed.messagingInvitation.direction == "incoming") {
                                console.log("direction invite")
                                msg.msgInviteState = embed.messagingInvitation._links.state;
                                msg.acceptInvite = embed.messagingInvitation._links['accept'].href;
                                msg.msgIn = embed.messagingInvitation._links.message.href;
                                break;
                            }                                   
                        }
                    };

                    if(link['rel'] == 'messaging') {
                        console.log('messaging there')
                        if(embed.messaging) {
                            if(embed.messaging.state == "Disconnected") {
                                console.log("msgn Disconnected")
                                msg.addMsg = embed.messaging._links.addMessaging.href;
                                break;
                            }
                        }
                    };
                    console.log(msg)                                              
                };                  
            };               
        });

我已经删除了结果验证并简化了for(事件中的var键)来处理两个操作。此外,我已经删除了我正在推送值的数组,因为我没有使用它。这可能是耗时的因素阻碍了我的方向验证。