简体   繁体   English

Node.js res.render()与请求不起作用

[英]Node.js res.render() with Request not working

I am trying to render an EJS template and pass in data to it with the node package Request. 我正在尝试渲染EJS模板,并使用节点包Request将数据传递给它。 I have got this to work with no problem using node-fetch on my last project. 我在上一个项目中使用node-fetch可以正常工作。

Here's a quick snippet of that: 这是一个简短的摘要:

const fetchCurrentWeather = (url, res) => {
   fetch(url)
      .then(res => res.json())
      .then(data => res.render('pages/weather', {
        currentId: data.list
      }))
      .catch(err => {
         console.log(err);
         res.sendStatus(500);
      });
}

And this will render the EJS weather page template with the data from the API response. 这将使用API​​响应中的数据呈现EJS天气页面模板。

With this latest project however, I am trying to use the Node Package Request to do the same thing and I am failing. 但是,在这个最新项目中,我尝试使用“节点包请求”执行相同的操作,但失败了。 Here's the code for that: 这是该代码:

app.post('/', (req, res, next) => {
    const paramOptions = {
          url: 'https://api.foursquare.com/v2/venues/search',
          method: 'GET',
          qs: {
            client_id: 'W033PDIYFI2TSAUO4L5ANUFOAUMZV32NUWNOF0NL0JS2E5W4',
            client_secret: 'SM1ZI11XOMPVMEGMBZXSN3LGTLOBQCVGIM1SN4QAO0QTCSM1',
            near: 'xxxxxx',
            intent: 'browse',
            radius: '15000',
            query: 'pizza',
            v: '20170801',
            limit: 1
          }
    };

    request(paramOptions, function(err, res, body) {
         if(err) {
           console.log(err)
         } else {
           res.render('pages/search'); //THIS WILL NOT WORK
           console.log(body) // RETURNS DATA FROM API ENDPOINT

        }
    });
    res.render('pages/search'); // THIS WILL WORK
});

The res.render() that does work is outside of the scope of the Request function, so I can not access the returned data from this Request function. res.render()不在Request函数的范围内,因此我无法从此Request函数访问返回的数据。 When I console.log the body of the Request function, I do get JSON data returned in my terminal view, so I know Request is working, but I am unsure of how to pass this data into an EJS template as I did with my fetch() example. 当我console.log Request函数的主体时,我确实在终端视图中返回了JSON数据,因此我知道Request可以正常工作,但是我不确定如何像fetch()数据一样将这些数据传递到EJS模板中。 fetch()示例。

There are two separate res parameters and the inner one is hiding the outer one. 有两个单独的res参数,而内部参数隐藏外部参数。 Change the name of the one in this line: 在此行中更改一个的名称:

request(paramOptions, function(err, res, body) {

to something else like this: 到这样的事情:

request(paramOptions, function(err, response, body) {

And, then when you call res.render() , it will use the higher scoped one you want. 然后,当您调用res.render() ,它将使用您想要的更高范围的对象。

You probably also need to pass some data to res.render(filename, data) as the second argument so you can feed the results of the request() to your render operation. 您可能还需要将一些数据作为第二个参数传递给res.render(filename, data) ,以便可以将request()的结果request()给渲染操作。

And, in your if (err) condition, you should send an error status, perhaps res.sendStatus(500) . 并且,在您的if (err)条件下,您应该发送错误状态,也许是res.sendStatus(500)

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

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