赋值时返回未定义的结果对象

时间:2019-01-31 12:37:21

标签: node.js

我正在创建一个结果对象以返回布尔值和字符串。它不会在if语句中重新分配。其余代码正常工作,并且密码已更新或正确的错误消息输出到控制台

我尝试在尝试捕获之前让结果保持不确定状态。

private async void PlayPausedAudio(object sender, EventArgs e)
{
    await CrossMediaManager.Current.PlaybackController.Play();
}

此方法正在调用代码

async function passwordUpdate(password, currentPassword, newPwd, email) {
  let hashedPassword = await bcrypt.hash(newPwd, 10);
  let result  = { success: false , message: ' '};

  try {
    bcrypt.compare(currentPassword, password, async function (err, res) {
      if (res) {
        let updateResult = await updatePwd(hashedPassword, email);
        if (updateResult) {
         result = { success: true , message: 'Password was updated successfully.'}; 
        }
        else {
          logger.info('Password was not updated successfully.');
        }
      } else {
        logger.error('Passwords do not match');
        result= { success: false , message: 'Your current password was entered incorrectly'};

        logger.error(result.message);
      }
    });
  } catch (error) {
    result= { success: false , message: 'Failed to compare passwords'}
  }
  logger.error('result ', result.message);
  return result;
}

logger.error(result.message); else语句中的这一行按预期输出消息,但
logger.error('result',result.message);尝试/捕获为result.message输出空白消息后

1 个答案:

答案 0 :(得分:1)

执行此操作时:

let result = await usersModel.passwordUpdate(pwd, cpwd, newPwd, email);

passwordUpdate函数根据该函数的“顶级”中的内容来解决诺言。这意味着bcrypt.compare的回调函数不会影响passwordUpdate的返回,这就是为什么您没有看到想要的结果的原因。

您可以做的是将整个内容包装在promise中,并在resolve函数内调用reject / bcrypt.compare

async function passwordUpdate(password, currentPassword, newPwd, email) {
  return new Promise(async function(resolve, reject) {
    let hashedPassword = await bcrypt.hash(newPwd, 10)

    try {
      bcrypt.compare(currentPassword, password, async function(err, res) {
        if (res) {
          let updateResult = await updatePwd(hashedPassword, email)
          if (updateResult) {
            resolve({
              success: true,
              message: 'Password was updated successfully.',
            })
          } else {
            reject({
              success: false,
              message: 'Password was not updated successfully.',
            })
          }
        } else {
          reject({
            success: false,
            message: 'Your current password was entered incorrectly',
          })
        }
      })
    } catch (error) {
      reject({ success: false, message: 'Failed to compare passwords' })
    }
  })
}

您还会注意到我删除了日志记录,可以根据需要将其放回去,但是由于这是一个承诺,因此您可以从调用者的.then.catch集中记录错误。或try / catch(如果您使用的是async / await

我还建议,由于您可以根据resolvereject来确定是否是错误,因此可以从结果中删除success并且只返回一个字符串,它将使代码更简洁:

async function passwordUpdate(password, currentPassword, newPwd, email) {
  return new Promise(async function(resolve, reject) {
    const hashedPassword = await bcrypt.hash(newPwd, 10)

    try {
      bcrypt.compare(currentPassword, password, async function(err, res) {
        if (res) {
          const updateResult = await updatePwd(hashedPassword, email)

          if (updateResult) {
            resolve('Password was updated successfully.')
          } else {
            reject('Password was not updated successfully.')
          }
        } else {
          reject('Your current password was entered incorrectly')
        }
      })
    } catch (error) {
      reject('Failed to compare passwords')
    }
  })
}

PS:我没有测试上面粘贴的代码,我只是修改了您的代码以更好地解释它。