nodejs,如何用变量接受request的返回值啊

这是我写的发送请求代码

var request = require('request');
var options = {
    'method': 'POST',
    'url': 'http://https://mail.georgebrown.ca/login',
    'headers': {
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'text/html, application/xhtml+xml, */*',
        
    },
    form: {
        'account': 'lch66',
        'password': '123456',
        'token': '-',
        '_time': '1622207143899',
    }
};
    request(options, function (error, response) {
    if (error) throw new Error(error);
    console.log(response.body);
});

 

我想用 一个变量接收response的值但直接return拿不到,只能在控制台输出,各位大佬能不能帮帮忙看看,萌新自学有点懵。十分感谢!!!

需要回调函数来接收。

function getSomeThings(xxxx,xxxx,callback){
  // doing somethings
  request.post(options,function(err,res,body){
    callback(err,body);
  });
}

 

我猜你是想用 async await 来做吧,类似下面的代码

export async function approvalListApi(params) {
  return axios({
    url: `${global.constants.baseUrl}/userCommit/list`,
    method: 'post',
    data: params,
  });
}




 getApprovalsData = async (params) => {
    this.setState({
      pageIndex: params.page,
      pageSize: params.size,
    });
    try {
      const response = await approvalListApi(params);
      const info = response.data;
      if (response.status === 200) {
        if (info.code === 200) {
          this.setState({
            dataSource: info.data.beans,
            total: info.data.total,
          });
        } else if (info.code === 402) {
          Message.warning("Login TimeOut,Please login again");
          this.props.gotoLogin();
        } else {
          Message.warning(info.message);
        }
      } else {
        Message.error("System Error");
      }
      return info;
    } catch (error) {
      Message.error("System Error");
    }
  };

 

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632