0

我已经使用 NodeJS/Express 设置了一个服务器应用程序,并试图让以下脚本工作,以便它将 http-request 的返回值存储在一个变量中以供以后使用。

function checkIfVerified(req, res) {
var user_id = req.user.id

var options = {
    method: 'GET',
    url: 'https://MYDOMAIN/api/v2/users/' + user_id + '?fields=email_verified&include_fields=true',
    headers:
        {
            authorization: 'Bearer ' + app.locals.token
        },
    body:
        {

        },
    json: true
}

function check(callback) {
    request(options, function (error, response, body) {
        return callback(null, body)
    })
}
var email_verified = check(function (err, data) {
    if (!err) {
        return data
    }
})
console.log(email_verified)
return email_verified
}

由于某种原因,变量“email_verified”没有任何价值......

谢谢你的帮助!干杯菲利普

4

1 回答 1

0

因此,在@Derlins 线索之后,我发现了以下问题的解决方案:

function check() {
  return new Promise((resolve, reject) => {
    request(options, (error, response, body) => {
      if (error) {
        reject(error)
      } else {
        resolve(body.email_verified)
      }
    })
  })
}
res.locals.email_verified = await check()

这可以解决问题:-)

于 2018-06-10T20:35:49.400 回答