在处理数据以查看时,未在eval上定义节点ejs参考错误数据 [英] node ejs reference error data not defined at eval when handing data to view

查看:50
本文介绍了在处理数据以查看时,未在eval上定义节点ejs参考错误数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用express和ejs关闭一个节点应用程序,但是当我尝试像这样从控制器将数据移交给我的视图时

i've been closing in on a node application using express and ejs, but when i try to hand data to my view from the controller like so

var myData = {
  theData: data
};
res.render(path.join(__dirname + '/../views/index'), myData);

我得到一个很好的错误


ReferenceError:.. myData is not defined eval (from ejs lib)

当试图像这样访问视图中的myData时

when trying to access myData in the view like so

var data = <%-myData%>;

或基本上以其他​​任何方式,我都尝试过对数据进行字符串化,将其包装在另一个对象之类的东西中,但是它仍然不会显示出来,我有一种感觉,我在这里确实缺少一些基本的东西,有谁知道如何解决这个问题?

or in any other way basically, i've tried stringifying the data, wrapping it in another object and stuff like that but it still just won't show up, i have the feeling i'm missing something really basic here, does anyone have an idea on how to fix this?

推荐答案

您传递给 render()的第二个参数是对象包含您想要的视图变量在您的模板中使用.因此,您看到的错误是有道理的.如果要以这种方式使用 myData ,则必须在控制器/应用中执行以下操作:

The second argument you pass to render() is an object containing the view variables you want to use in your template. So the error you are seeing makes sense. If you want to use myData in that way you'd have to do something like this in your controller/app:

res.render(..., { myData: JSON.stringify(myData) });

这篇关于在处理数据以查看时,未在eval上定义节点ejs参考错误数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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