node.js - 对多个基本 node.js 请求感到困惑
问题描述
您好我正在尝试将两个 api 请求合并到我的简单 node.js 网站上。我让它们都单独工作,但我正在努力将它们结合起来在同一页面上工作。
request(url2, function (err, response, body) {
if(err){
res.render('index', {news: null, error: 'Error, please try again',initials111:initials,test:potatoes});
} else {
let news = JSON.parse(body);
if(news== undefined){
res.render('index', {news: null, error: 'Error, please try again',initials111:initials,test:potatoes});
}
else{
let newsText = `${news.articles[0].title}` ;
res.render('index', {news: newsText, error: null,initials111:initials,test:potatoes});
}
}
}
);
request(url, function (err, response, body) {
if(err){
res.render('index', {weather: null, error: 'Error, please try again',initials111:initials,test:potatoes});
} else {
let weather = JSON.parse(body);
if(weather.main == undefined){
res.render('index', {weather: null, error: 'Error, please try again',initials111:initials,test:potatoes});
} else {
if (rain == "0,rain"){
let weatherText = `It's ${weather.main.temp} degrees celsius and with wind speeds of ${weather.wind.speed} mph in ${weather.name} ${weather.sys.country}! & ${weather.weather[0].description}` ;
res.render('index', {weather: weatherText, error: null,initials111:initials,test:potatoes});
}
else{
let weatherText = `It's ${weather.main.temp} degrees celsius and with wind speeds of ${weather.wind.speed} mph in ${weather.name} ${weather.sys.country}!` ;
res.render('index', {weather: weatherText, error: null,initials111:initials,test:potatoes});
}
}
}
});
这些是每个请求的两个代码部分。如果需要,我可以附上网站的屏幕截图。如果我将它们都放在 server.js 文件中,则会出现很多错误。任何人都可以帮助/提供提示吗?
例如,当我将它们都粘贴到 server.js 并尝试该网站时,我收到此错误
ReferenceError: C:\NodeShit\weather-rough\views\index.ejs:42
40|
41|
>> 42| <% if(news !== null){ %>
43| <p><%= news %></p>
44| <% } %>
45|
news is not defined
at eval (eval at compile (C:\NodeShit\weather-rough\node_modules\ejs\lib\ejs.js:618:12), <anonymous>:11:8)
at returnedFn (C:\NodeShit\weather-rough\node_modules\ejs\lib\ejs.js:653:17)
at tryHandleCache (C:\NodeShit\weather-rough\node_modules\ejs\lib\ejs.js:251:36)
at View.exports.renderFile [as engine] (C:\NodeShit\weather-rough\node_modules\ejs\lib\ejs.js:482:10)
at View.render (C:\NodeShit\weather-rough\node_modules\express\lib\view.js:135:8)
at tryRender (C:\NodeShit\weather-rough\node_modules\express\lib\application.js:640:10)
at Function.render (C:\NodeShit\weather-rough\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\NodeShit\weather-rough\node_modules\express\lib\response.js:1008:7)
at Request._callback (C:\NodeShit\weather-rough\server.js:63:13)
at Request.self.callback (C:\NodeShit\weather-rough\node_modules\request\request.js:185:22)
解决方案
推荐阅读
- excel - 如何根据其旁边的单元格中的值更改来修改单元格值
- java - 如何在java常量中传递jenkins参数值
- java - 如果消费者线程面临异常,如何停止生产者线程
- python - Python (Pytorch) - 循环和日志记录
- reactjs - Next.js 在下一页的 getInitialProps 完成加载时保持当前页面处于活动状态并重新渲染
- java - 弹簧一对一的关系
- azure - 新 Microsoft.Azure.Cosmos SDK 中的 CompositeToken
- ruby-on-rails - 如何在用户登录前允许查看指定资源?
- postgresql - 使用 PostgresSQL 动态转置表
- java - 如何创建我自己的 PageSize 变量以使用 iTextpdf 创建 PDF 文档