简体   繁体   English

如何修复返回未定义的代码?

[英]How to fix code that returns undefined?

I have this code:我有这个代码:

const liked_users = promises[1];
    const disliked_users = promises[0];
    if (liked_users.length > 0 || disliked_users.length > 0){
      for(var i = 0; i < liked_users.length; i++){
        for(var j = 0; j < disliked_users.length; j++){
          for(var k = 0; k < _USERS.length; k++){
            if(_USERS[k].useruid == liked_users[i].likedUseruid || disliked_users[j].dislikedUseruid){
              _USERS.splice(i, 1);
              i--;
              break;
            }

basically what is happening is that I access the firebase database and I pull out some data from my objects.基本上发生的事情是我访问了 firebase 数据库并从我的对象中提取了一些数据。

The problem comes where sometimes liked_users is going to be blank and therefore liked_users[i].likedUseruid will return undefined.问题是有时like_users 会是空白的,因此liked_users[i].likedUseruid将返回undefined。 When they are defined, the code runs fine.当它们被定义时,代码运行良好。

How can I put in some conditional or block of code that allows it to be read in a way that accepts it can be undefined or doesn't run the code until it is defined?我怎样才能放入一些条件或代码块,以允许它以一种接受它可以未定义或在定义之前不运行代码的方式读取? I can show more code if it will help.如果有帮助,我可以显示更多代码。

In JavaScript if you put just variable name in if condition then it will check its available or not.在 JavaScript 中,如果你只在if condition变量名,那么它会检查它是否可用。

For example put this block例如把这个块

if(_USERS[k].useruid == liked_users[i].likedUseruid || disliked_users[j].dislikedUseruid){
....
}

in this在这

if(_USERS[k].useruid && liked_users[i].likedUseruid &&  disliked_users[j].dislikedUseruid){
....
}

The above condition will check that _USERS[k].useruid is available or not and will continue...上述条件将检查_USERS[k].useruid是否可用,并将继续......

You can make condition as per your need.您可以根据需要制作条件。

I hope this will help you.我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM