传递数据以在node + express中查看 [英] Passing data to view in node + express

查看:145
本文介绍了传递数据以在node + express中查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用我的节点应用程序从API中检索一些数据,并希望在视图中显示该数据,并允许用户将其更改。我打电话给api,并得到一个JSON响应,我想传递到视图。这是我正在做的:

  var query ='SELECT Id,OwnerId,ActivityDateTime,EndDateTime,WhatId,WhoId,Subject FROM事件WHERE OwnerId = \''+ user.userId +'\''; 
//执行查询并获取响应
...
...
var ev = resp.records;
res.render('index.html',{eventData:ev});问题是当我想在我的视图中使用数据时,我会收到一个未定义的错误。



如果我这样做:

 < div id ='主题>#{eventData.Subject}< / DIV> 

我打印了#{eventData.Subject}



尝试使用JS中的数据返回一个未定义的错误:

 < script type =text / JavaScript的> 
var mydata =#{eventData};
< / script>

经过一些搜索,我发现一个建议首先将其进行字符化的帖子:

  var mydata =!{JSON.stringify(eventData)}; 
console.log(mydata);

这里,我收到语法错误SyntaxError:missing:after property id



我很确定我错过了一些基本的东西,不能为我的生活弄清楚。我发现很多文章的例子说明了如何在Jade中进行操作,但是我不想使用Jade,并且使用文章中的未定义或非法字符错误的建议。



我完全不了解...帮助?
Tnx

解决方案

Jonathan Ong的评论实际上使我走上正轨。由于我使用的客户处理程序是html,它不会解释任何变量。



我切换到使用ejs:

  //查看引擎ejs 
app.set('view engine','ejs');
app.register('。html',require('ejs'));

并使用以下占位符:

 <%= eventData.Subject%> 

这实际上使我的数据显示。感谢所有的意见和答案!
Michael


I'm using my node app to retrieve some data from an API and want to display that data in a view and allow the user to change it there. I'm calling the api and get a JSON response which I'd like to pass onto the view. Here's what I'm doing:

    var query = 'SELECT Id, OwnerId, ActivityDateTime, EndDateTime, WhatId, WhoId, Subject FROM Event WHERE OwnerId = \'' + user.userId + '\''  ;
    // execute the query and get response
    ...
    ...
    var ev = resp.records;
    res.render('index.html', {eventData : ev});

Problem being that when I then want to use the data in my view, I get an undefined error.

If I do:

    <div id='Subject'>#{eventData.Subject}</div>

I get a print-out of #{eventData.Subject}

Trying to use the data in JS returns an undefined error:

    <script type="text/javascript">
    var mydata = #{eventData};
    </script>

After some searching I found a post that suggested to stringify it first:

    var mydata = !{JSON.stringify(eventData)};
    console.log(mydata);

Here, I get a syntax error "SyntaxError: missing : after property id"

I'm pretty sure I'm missing something basic and can't for the life of me figure it out. I've found a lot of articles examples that explain how to do it in Jade, but I don't want to use Jade and using the recommendations from the articles results in "undefined" or "illegal character" errors.

I'm all out of ideas... help? Tnx

解决方案

Jonathan Ong's comment actually brought me onto the right track. Since I was using a customer handler for html, it wouldn't interpret any variables.

I switched to using ejs:

     // view engine ejs
     app.set('view engine', 'ejs');
     app.register('.html', require('ejs'));

and used the following placeholder:

    <%= eventData.Subject %>

This actually made my data show up. Thanks for all the comments and answers! Michael

这篇关于传递数据以在node + express中查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆