将JSON值传递到车把视图

问题描述

我正在使用带有把手的NodeJs和express.js做一个应用程序,我请求一个具有unirest的JSON对象,然后我需要将图像url作为该JSON对象中的值并将其传递给res .render以将其作为链接传递到视图。但是我在视图中传递链接时看不到链接。 这是代码

home.handlebars:

<p>home</p>
<p>{{name}}</p>
<img src={{path}}/>

index.js:

router.get('/',function(req,res,next) {
  var ist = istance_request();
  ist.end(function (response) {
    if (response.error) throw new Error(response.error);

    
    //the url is at response.body[1].img
    res.render('home',{name : response.body[1].name,path: response.body[1].img });
  });
});

在html中显示以下内容

<!DOCTYPE html>
<html>
<head>
    <Meta charset="utf-8">
    <title>Example App</title>
</head>
<body>

    <p>home</p>
    <p></p>
    <img src=/>

</body>
</html>

所以,如果我尝试做这样的事情:

//not work
res.render('home',path: response.body[1].img });

//not work
var img = response.body[1].img;
res.render('home',{name: response.body[1].name,path: img)

//not work
var img = JSON.stringify(response.body[1].img);
res.render('home',path: img)

//work
res.render('home',path: 'url_of_the_image')

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)