express-ejs-layout 有时无法检测到传递参数
Posted
技术标签:
【中文标题】express-ejs-layout 有时无法检测到传递参数【英文标题】:express-ejs-layout sometimes fails to detect passing argument 【发布时间】:2017-04-01 16:05:01 【问题描述】:我正在渲染一个页面,其中我正在传递数据“userInfo”,但有时它会渲染,有时它会显示此 ReferenceError,不确定为什么?我的问题是,如果有任何错误,那么它根本不应该工作。我该如何解决?
<pre>
ReferenceError: /Users/user/Desktop/iSlamic/views/layout.ejs:77
75| </li>
76|
>> 77| if(userInfo)
78| <li class="dropdown">
79| <a href="#" class="dropdown-toggle" data-toggle="dropdown">Motiur<b class="caret"></b></a>
80| <ul class="dropdown-menu">
userInfo is not defined
at eval (eval at <anonymous> (/Users/user/Desktop/iSlamic/node_modules/ejs/lib/ejs.js:491:12), <anonymous>:11:10)
at returnedFn (/Users/user/Desktop/iSlamic/node_modules/ejs/lib/ejs.js:520:17)
at View.exports.renderFile [as engine] (/Users/user/Desktop/iSlamic/node_modules/ejs/lib/ejs.js:374:31)
at View.render (/Users/user/Desktop/iSlamic/node_modules/express/lib/view.js:126:8)
at tryRender (/Users/user/Desktop/iSlamic/node_modules/express/lib/application.js:639:10)
at EventEmitter.render (/Users/user/Desktop/iSlamic/node_modules/express/lib/application.js:591:3)
at ServerResponse.render (/Users/user/Desktop/iSlamic/node_modules/express/lib/response.js:961:7)
at /Users/user/Desktop/iSlamic/node_modules/express-ejs-layouts/lib/express-layouts.js:108:14
at View.exports.renderFile [as engine] (/Users/user/Desktop/iSlamic/node_modules/ejs/lib/ejs.js:379:10)
at View.render (/Users/user/Desktop/iSlamic/node_modules/express/lib/view.js:126:8)
at tryRender (/Users/user/Desktop/iSlamic/node_modules/express/lib/application.js:639:10)
at EventEmitter.render (/Users/user/Desktop/iSlamic/node_modules/express/lib/application.js:591:3)
at ServerResponse.render (/Users/user/Desktop/iSlamic/node_modules/express/lib/response.js:961:7)
at ServerResponse.res.render (/Users/user/Desktop/iSlamic/node_modules/express-ejs-layouts/lib/express-layouts.js:77:12)
at /Users/user/Desktop/iSlamic/app.js:97:7
at Layer.handle_error (/Users/user/Desktop/iSlamic/node_modules/express/lib/router/layer.js:71:5)
</pre>
<%if(userInfo)%>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Motiur<b class="caret"></b></a>
<ul class="dropdown-menu">
<li>
<a href="/admin/insert">Insert</a>
</li>
<li>
<a href="/admin/update">Update</a>
</li>
<li>
<a href="/admin/delete">Delete</a>
</li>
<li>
<a href="/logout">Logout</a>
</li>
</ul>
</li>
<% else %>
<li>
<a href="/admin/login">Login</a>
</li>
<%%>
My js file
<pre>
router.get('/speaker/:name', function(req, res, next)
var video_Data;
was.find("name":req.params.name, "wasType":"video").sort(_id:-1).limit(5).exec(function(err,videoDocs)
if(err)
res.json(err)
mongoose.connection.close();
else
video_Data = videoDocs;
);
//var audioData;
was.find("name":req.params.name, "wasType":"audio").sort(_id:-1).limit(5).exec(function(err,docs)
if(err)
res.json(err);
mongoose.connection.close();
else
if(video_Data==null&&docs==null)
res.json("Data does not exist");
else
res.render('video2', "data":docs,"videoData": video_Data,"userInfo":req.session.admin);
);
);
我通过 userInfo 传递布尔值。
【问题讨论】:
video_Data
在您调用 res.render
时可能不会被填充。
@noisypixy ,如果我重新加载 5 次,则 3 次加载成功,其他 2 次失败显示该错误。我该如何处理和解决这种情况?
【参考方案1】:
我得到了答案。 @noisypixy 你是对的,我的 video_Data 没有正确填充。当我渲染我的视图时,有时视频数据没有得到。所以我更改了我的代码,现在它可以按预期工作了。
router.get('/speaker/:name', function(req, res, next) var video_Data; was.find("name":req.params.name, "wasType":"video").sort(_id:-1).limit(5).exec(function(err,videoDocs) 如果(错误) res.json(错误) 猫鼬.connection.close(); 别的 video_Data = videoDocs; was.find("name":req.params.name, "wasType":"audio").sort(_id:-1).limit(5).exec(function(err,docs) 如果(错误) res.json(错误); 猫鼬.connection.close(); 别的 如果(video_Data==null&&docs==null) res.json("数据不存在"); 别的 res.render('video2', "data":docs,"videoData": video_Data,"userInfo":req.session.admin); ); ); );【讨论】:
以上是关于express-ejs-layout 有时无法检测到传递参数的主要内容,如果未能解决你的问题,请参考以下文章