如何将异步输出提供给表达渲染器?
how to feed the async output to express renderer?
我请求获取示例 XML 文件,对其进行解析,现在我想将其传递给 Express 渲染器以在页面上动态显示该值。我的渲染器路由器:
app.get('/dynamic_view', function(req, res){
res.render('dynamic', {
name: //pass my output here
});
});
我得到这样的异步输出:
const parseString = require('xml2js').parseString;
const axios = require('axios');
let url = 'https://www.w3schools.com/xml/simple.xml';
axios.get(url)
.then(response => {
parseString(response.data, function (err, result) {
console.log(result.breakfast_menu.food[0].name); // returns 'Belgian Waffles'
});
})
如何将我的异步变量输出(应该是 'Belgian Waffles')传递给 res.renderer
的 name
属性?
const parseString = require('xml2js').parseString;
const axios = require('axios');
let url = 'https://www.w3schools.com/xml/simple.xml';
app.get('/dynamic_view', function(req, res, next){
axios.get(url)
.then(response => {
parseString(response.data, function (err, result) {
if (err) return next(err)
res.render('dynamic', {
name: result.breakfast_menu.food[0].name
});
});
})
});
我请求获取示例 XML 文件,对其进行解析,现在我想将其传递给 Express 渲染器以在页面上动态显示该值。我的渲染器路由器:
app.get('/dynamic_view', function(req, res){
res.render('dynamic', {
name: //pass my output here
});
});
我得到这样的异步输出:
const parseString = require('xml2js').parseString;
const axios = require('axios');
let url = 'https://www.w3schools.com/xml/simple.xml';
axios.get(url)
.then(response => {
parseString(response.data, function (err, result) {
console.log(result.breakfast_menu.food[0].name); // returns 'Belgian Waffles'
});
})
如何将我的异步变量输出(应该是 'Belgian Waffles')传递给 res.renderer
的 name
属性?
const parseString = require('xml2js').parseString;
const axios = require('axios');
let url = 'https://www.w3schools.com/xml/simple.xml';
app.get('/dynamic_view', function(req, res, next){
axios.get(url)
.then(response => {
parseString(response.data, function (err, result) {
if (err) return next(err)
res.render('dynamic', {
name: result.breakfast_menu.food[0].name
});
});
})
});