繁体   English   中英

从Unirest Node get request函数发送响应到Jade视图

[英]Send response from Unirest Node get request function to Jade view

我正在构建我的第一个Node应用程序,并且在处理unirest.get请求时遇到了麻烦。 我的项目是使用Node,Express,Node和Act On API构建的。

我正在使用Express Generator快速启动地面项目。

我遇到的问题是我正在努力将响应传递到路由文件中。 我正在从Act On API请求一个列表,该列表已正确返回,因为我在登录时可以在控制台中看到响应,但无法将数据传递到模板。

function getTheList(callback) {
var Request = unirest.get('https://restapi.actonsoftware.com/api/1/list/l-0001')
.headers({
    'Accept': 'application/json',
    'Authorization': 'Bearer ' + access_token
})
.query({
    "count": 20,
    "fields": "First Name;Last Name;Email;"
})
.end(function(response, error) {
    var data = response.body.data;
    if (!error && response.statusCode == 200) {
        callback(returnData(data)); 
    } else {
        console.log('Failed response');
    }
});
}

function returnData(theData){
  console.log(theData);
  return theData;
}

module.exports.get = getTheList;

以及我的路线文件中的代码以获取此信息。

var masterList = require('../acton/getMasterList');

var myListVar = masterList.get();

任何对我做错事的帮助将不胜感激。

您描述的getTheList函数需要一个回调,而在调用它时不会像masterList.get()

因此,您可以执行以下操作:

masterList.get(function(data){
    //you can access data here.
})

或者,在getTheList实现中完全取消回调。

 .end(function(response, error) {
    var data = response.body.data;
    if (!error && response.statusCode == 200) {
      returnData(data); //Just do this may be.
    } else {
      console.log('Failed response');
    }
});

暂无
暂无

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

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