无法在Nodejs中正确使用if else条件

7cwmlq89  于 2023-04-11  发布在  Node.js
关注(0)|答案(1)|浏览(161)

我正在使用Nodejs和“express js”,现在我正在获取用户列表(在console.log中),现在我想将此数据传递给“model”,但在console.log中显示我“undefined”,我如何解决这个问题?这是我的控制器代码

const sendotps = async (req, res) => {
    var phone_number=req.body.phone_number;
    const result= await User.sendOtp(phone_number);
    console.log(result); // GETTING UNDEFINED HERE
}

Here is my model file

 

    static sendOtp(phone_number) {
            var otp=Math.floor(1000 + Math.random() * 9999);
            
            var config = {
            method: 'get',
            maxBodyLength: Infinity,
              url: 'http://2factor.in/API/V1/xxxxxxxx/SMS/+xxxxxxx/'+otp+'/XXXXXXXX',
              headers: { }
            };
             return axios(config)
            .then(async function (response) {
                
                const sql = "SELECT phoneNumber FROM verifyOtp WHERE phoneNumber = '${phone_number}'";
                const [rows, fields] = await pool.execute(sql);
                if(rows>0)
                {
                    console.log('total rows are more than zero'+ rows);
                }
                else
                {
                    const sql = `INSERT INTO verifyOtp (phoneNumber, otp, userType) VALUES ("${phone_number}", "${otp}", "2")`;
                    await pool.execute(sql);
    const sql2 = `SELECT * FROM users WHERE phone_number = '${phone_number}' AND is_registered='1'`;
                    const [rows2, fields2] = await pool.execute(sql2);
                    console.log(rows2);
                   if(rows2>0)
                    {
                            return rows2;
                    }
                    else
                    {
                            return false;
                    }
    
    
                }   
    
            })
            .catch(function (error) {
              console.log(error);
              
            });
            
        }
7fhtutme

7fhtutme1#

你还需要返回axios调用。return in then块将数据返回给axios调用,但是你需要将这些数据返回给函数调用。

相关问题