简体   繁体   English

Node.js:REST调用和请求函数返回未定义的值

[英]Node.js: REST Call and Request Function returns undefined values

I have been trying to learn the MEAN stack and ran into some issues with the javascript below that is in node.js. 我一直在尝试学习MEAN堆栈,并在node.js中的以下javascript遇到了一些问题。 I have been having trouble with my module.exports.homelist function in which the functions, like function (err, response, body), have been giving me those values as undefined. 我在我的module.exports.homelist函数中遇到了麻烦,其中的函数(例如函数(err,response,body))给了我那些未定义的值。 I have been searching for answers for a while and came across asynchronous code and the callback function, but I was unable to find the solution that fits my situation, especially when the function is called on from within a request. 我一直在寻找答案,遇到了异步代码和回调函数,但是找不到适合我情况的解决方案,尤其是在从请求中调用该函数时。

The Code: 编码:

var request = require('request');
var apiOptions = {
    server : "https://localhost:3000"
};
if (process.env.NODE_ENV === 'production') {
    apiOptions.server = "https://getting-mean-loc8r.herokuapp.com";
}

var renderHomepage = function (req, res, responseBody) {
    var message;
    if (!(responseBody instanceof Array)) {
        message = "API lookup error";
        responseBody = [];
    } else {
        if (!responseBody.length) {
            message = "No places found nearby";
        }
    }
    res.render('locations-list', {
        title: 'Loc8r - find a place to work with wifi',
        pageHeader: {
            title: 'Loc8r',
            strapline: 'Find places to work with wifi near you!'
        },
        sidebar: "Looking for wifi and a seat? Loc8r helps you find places to work when out and about. Perhaps with coffee, cake or a pint? Let Loc8r help you find the place you're looking for.",
        locations: responseBody,
        message: message
    });
}

/* GET 'home' page */
module.exports.homelist = function(req, res) {
    var requestOptions, path;
    path = '/api/locations';
    requestOptions = {
        url : apiOptions.server + path,
        method : "GET",
        json : {},
        qs : {
            lng : -0.7992599,
            lat : 51.378091,
            maxDistance : 20
        }
    };
    request(
        requestOptions,
        function(err, response, body) {
            var i, data;
            data = body;
            if (data !== undefined && response !== undefined && response.statusCode === 200 && data.length) {
                for (i=0; i<data.length; i++) {
                    data[i].distance = _formatDistance(data[i].distance);
                }
            }
            renderHomepage(req, res, data);
        }
    );

    var _formatDistance = function (distance) {
        var numDistance, unit;
        if (distance > 1) {
            numDistance = parseFloat(distance).toFixed(1);
            unit = 'km';
        } else {
            numDistance = parseInt(distance * 1000,10);
            unit = 'm';
        }
        return numDistance + unit;
    }
};

EDIT: This is the code I have in another file that uses my homelist function to render an HTML homepage: 编辑:这是我在另一个文件中使用我的清单功能来呈现HTML主页的代码:

var express = require('express'); var router = express.Router(); 
var ctrlLocations = require('../controllers/locations');
router.get('/', ctrlLocations.homelist);
module.exports = router;

You mention MEAN stack- are you requiring express? 您提到MEAN堆栈-您需要快递吗? Please read the documentation on the express website. 请阅读快递网站上的文档。

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

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