如何分配返回数据对象,以便可以在视图中呈现它(node.js) [英] How to assign return data object so it can be rendered in view (node.js)

查看:72
本文介绍了如何分配返回数据对象,以便可以在视图中呈现它(node.js)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,基本上是在向我的api请求以检索JSON数据并导出此自定义模块.

I have the following code and I'm basically making a request to my api to retrieve JSON data and exporting this custom module.

问题在于,单独使用var data = require('./lib/data.js');,数据实际上并没有我期望的数据.

The problem is that with var data = require('./lib/data.js'); alone, data doesn't really have the data I expected to have.

如果它是项目中的物理JSON文件,则可以执行var data = require('./data.json');,但不能使用这种静态数据.

If it was a physical JSON file sitting in the project, I could do var data = require('./data.json'); but I don't work with this kind of static data.

这是怎么了?

data.js (我的用于获取JSON数据的自定义模块)

data.js (my custom module to get JSON data)

module.exports = function() {

    var request = require("request")
    var url = "http://sheetsu.com/apis/94dc0db4"

    request({
        url: url,
        json: true
    }, function (error, response, body) {

        if (!error && response.statusCode === 200) {
            console.log(body)
        }
    })

}

index.js

var express = require('express');
var router = express.Router();

var data = require('./lib/data.js');

router.get('/', function(req, res, next) {
  res.render('index', { 
    title: 'Express' 
    data: data
  });
});

module.exports = router;

推荐答案

这里有几件事情要考虑:

There are a couple of things to consider here:

  1. 您对服务器的请求是异步的
  2. 您实际上并没有将数据返回到module.exports
  3. 代码中的一些基本语法错误

尝试一下:

data.js

module.exports = function(callback) {

    var request = require("request")
    var url = "http://sheetsu.com/apis/94dc0db4"

    request({ url: url, json: true }, function (error, response, body) {
        if (!error && response.statusCode === 200) {
            callback( body );
        }
    });
}

index.js

var express = require('express');
var router = express.Router();

var data = require('./lib/data.js');

data( function(data) {

    router.get('/', function(req, res, next) {

        res.render('index', { title: 'Express', data: data });

    });
});

module.exports = router;

更多细节;获取JSON数据的请求将不会同步.这意味着花费任何时间来回复数据,因此它使您的程序可以完成其当前线程.这意味着,当您的index.js文件正在接收data.js模块时,数据可能尚未到达.您可以在此处了解更多有关异步与同步的信息.

In a bit more detail; the request to get your JSON data will not be synchronous. That means that it can take any amount of time to respond back with the data, so it allows your program to finish its current thread. This means that when your index.js file is receiving the data.js module, the data may not have arrived yet. You can read more about asynchronous vs synchronous here.

我们如何解决这个问题?我们为该方法传递一个callback函数以在完成时执行.

How do we get around this problem? We pass the method a callback function to execute when it is finished.

当我们执行模块时...

When we execute the module...

data( function(data) { ...

...我们正在传递给它一个从服务器接收数据时要运行的函数.如果服务器没有错误,并且statusCode没问题...

...We're passing it a function to run when it receives the data from the server. If there is no error from the server and the statusCode is OK...

if (!error && response.statusCode === 200) { ...

...然后我们调用传递的callback函数,将body(或数据,如果您愿意)作为参数传递...

... Then we call the callback function that was passed, passing the body (or data, if you like) as an argument...

callback( body );

...然后我们可以在index.js ...

...We can then use this in our index.js...

res.render('index', { title: 'Express', data: data });

这篇关于如何分配返回数据对象,以便可以在视图中呈现它(node.js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆