Javascript 节点JS未向模板显示我的数据
我正在查询YelpAPI中的一些数据。业务的Console.log在终端中工作正常,但当我调用同一个变量在模板中呈现时,什么也不显示Javascript 节点JS未向模板显示我的数据,javascript,jquery,node.js,pug,Javascript,Jquery,Node.js,Pug,我正在查询YelpAPI中的一些数据。业务的Console.log在终端中工作正常,但当我调用同一个变量在模板中呈现时,什么也不显示 /* GET home page. */ router.get('/', function(req, res) { res.render('index', { title: 'Express' }); }); /*GET Hello World page. */ router.get('/helloworld', function(req, res){
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});
/*GET Hello World page. */
router.get('/helloworld', function(req, res){
var first;
yelp.search({location: "tempe", term: "sandwiches", category_filter: "food"}, function(error, data) {
var test = JSON.stringify(data); //Changes yelps response to JSON Objects with double quotes
test = JSON.parse(test); //JSON.parse can now parse correctly after stringify is used.
// for(var i = 0; i < 9; i++){
// console.log(test['businesses'][i]['name']);
// // console.log(test['businesses'][i]);
// }
console.log(test['businesses'][0]['name']);
first = test['businesses'][0]['name'];
});
res.render('helloworld', {
title: 'Hello, World',
name: first
});
});
module.exports = router;
/*获取主页*/
router.get('/',函数(req,res){
res.render('index',{title:'Express'});
});
/*获取Hello世界页面*/
router.get('/helloworld',函数(req,res){
var优先;
yelp.search({位置:“tempe”,术语:“三明治”,类别过滤器:“食品”},功能(错误,数据){
var test=JSON.stringify(data);//使用双引号更改对JSON对象的yelps响应
test=JSON.parse(test);//使用stringify后,JSON.parse现在可以正确解析。
//对于(变量i=0;i<9;i++){
//log(test['business'][i]['name']);
////console.log(test['business'][i]);
// }
log(测试['business'][0]['name']);
第一个=测试['business'][0]['name'];
});
res.render('helloworld'{
标题:“你好,世界”,
姓名:第一
});
});
module.exports=路由器;
yelp.search是异步的。您必须在回调中调用res.render
,如:
yelp.search({location: "tempe", term: "sandwiches", category_filter: "food"}, function(error, data) {
var test = JSON.stringify(data); //Changes yelps response to JSON Objects with double quotes
test = JSON.parse(test); //JSON.parse can now parse correctly after stringify is used.
// for(var i = 0; i < 9; i++){
// console.log(test['businesses'][i]['name']);
// // console.log(test['businesses'][i]);
// }
console.log(test['businesses'][0]['name']);
first = test['businesses'][0]['name'];
res.render('helloworld', {
title: 'Hello, World',
name: first
});
});
yelp.search({location:“tempe”,term:“sandwiches”,category_filter:“food”},函数(错误,数据){
var test=JSON.stringify(data);//使用双引号更改对JSON对象的yelps响应
test=JSON.parse(test);//使用stringify后,JSON.parse现在可以正确解析。
//对于(变量i=0;i<9;i++){
//log(test['business'][i]['name']);
////console.log(test['business'][i]);
// }
log(测试['business'][0]['name']);
第一个=测试['business'][0]['name'];
res.render('helloworld'{
标题:“你好,世界”,
姓名:第一
});
});
,否则将在返回
yelp.search
之前调用它。yelp.search
是异步的。您必须在回调中调用res.render
,如:
yelp.search({location: "tempe", term: "sandwiches", category_filter: "food"}, function(error, data) {
var test = JSON.stringify(data); //Changes yelps response to JSON Objects with double quotes
test = JSON.parse(test); //JSON.parse can now parse correctly after stringify is used.
// for(var i = 0; i < 9; i++){
// console.log(test['businesses'][i]['name']);
// // console.log(test['businesses'][i]);
// }
console.log(test['businesses'][0]['name']);
first = test['businesses'][0]['name'];
res.render('helloworld', {
title: 'Hello, World',
name: first
});
});
yelp.search({location:“tempe”,term:“sandwiches”,category_filter:“food”},函数(错误,数据){
var test=JSON.stringify(data);//使用双引号更改对JSON对象的yelps响应
test=JSON.parse(test);//使用stringify后,JSON.parse现在可以正确解析。
//对于(变量i=0;i<9;i++){
//log(test['business'][i]['name']);
////console.log(test['business'][i]);
// }
log(测试['business'][0]['name']);
第一个=测试['business'][0]['name'];
res.render('helloworld'{
标题:“你好,世界”,
姓名:第一
});
});
,否则将在返回
yelp.search
之前调用它。这是异步回调。
当yelp.search
exec,并且回调仍然不是exec时,name
是未定义的。因此,您的渲染{name:'undefined'}。
除了@Rodrigo Medeiros的方法外,您还可以使用一些模块来解决。e、 g:
q
,asnyc
,bluebird
等等。这是异步回调。
当yelp.search
exec,并且回调仍然不是exec时,name
是未定义的。因此,您的渲染{name:'undefined'}。
除了@Rodrigo Medeiros的方法外,您还可以使用一些模块来解决。e、 g:
q
、asnyc
、bluebird
等等。将值赋给first之后,尝试在回调函数中添加要呈现的调用。按照现在的编写方式,res.render在首次分配值之前执行,这可能是什么也不显示的原因。请在将值分配给第一个值后,尝试在回调函数中添加对render的调用。按照现在的编写方式,res.render在第一次分配值之前执行,这可能就是为什么什么也不显示。