Mongodb 在forEach循环中未定义Sails、EJS数据
我正在使用基于@bradtraversy教程的MongoDB开发Sails.js CRUD应用程序(文章管理)。我在尝试使用EJS语法和forEach循环显示数据库中的文章时遇到了这个问题。错误为“未定义文章数据”。 基本上,我通过SailsMongodb 在forEach循环中未定义Sails、EJS数据,mongodb,express,sails.js,ejs,sails-mongo,Mongodb,Express,Sails.js,Ejs,Sails Mongo,我正在使用基于@bradtraversy教程的MongoDB开发Sails.js CRUD应用程序(文章管理)。我在尝试使用EJS语法和forEach循环显示数据库中的文章时遇到了这个问题。错误为“未定义文章数据”。 基本上,我通过Sails/Create?title=ArticleOne%20body=BodyOneURL蓝图将示例记录传递给Mongo。这篇文章可以在Mongo中清晰地看到 datastores.js是为Mongo配置的: module.exports.datastores
/Create?title=ArticleOne%20body=BodyOne
URL蓝图将示例记录传递给Mongo。这篇文章可以在Mongo中清晰地看到
datastores.js是为Mongo配置的:
module.exports.datastores = {
mongodb: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
database: 'articlebase'
},
};
routes.js指出list.ejs视图:
module.exports.routes = {
'/': { view: 'pages/homepage' },
'/articles/list': { view: 'pages/list' },
'/articles/add': { view: 'pages/add' },
};
<h1 class="display-4">Articles</h1>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th></th>
</tr>
</thead>
<tbody>
<% articles.forEach(function(article){ %>
<tr>
<td><%= article.id %></td>
<td><%= article.title %></td>
<td>
<a href="/articles/edit/<%= article.id %>" class="btn btn-primary">Edit</a>
<form class="d-inline" action="/articles/delete/<%= article.id %>" method="post">
<input type="submit" value="Delete" class="btn btn-danger">
</form>
</td>
</tr>
<% }); %>
</tbody>
</table>
Article.js模型:
module.exports = {
attributes: {
title:{
type: 'string'
},
body:{
type: 'string'
}
},
datastore: 'mongodb'
};
ArticlesController.js
module.exports = {
list:function(req, res){
Articles.find({}).exec(function(err, articles){
if(err){
res.send(500, {error: 'Database Error'});
}
res.view('list', {articles:articles});
});
},
add: function(req, res){
res.view('add');
}
};
最后是棘手的list.ejs视图:
module.exports.routes = {
'/': { view: 'pages/homepage' },
'/articles/list': { view: 'pages/list' },
'/articles/add': { view: 'pages/add' },
};
<h1 class="display-4">Articles</h1>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th></th>
</tr>
</thead>
<tbody>
<% articles.forEach(function(article){ %>
<tr>
<td><%= article.id %></td>
<td><%= article.title %></td>
<td>
<a href="/articles/edit/<%= article.id %>" class="btn btn-primary">Edit</a>
<form class="d-inline" action="/articles/delete/<%= article.id %>" method="post">
<input type="submit" value="Delete" class="btn btn-danger">
</form>
</td>
</tr>
<% }); %>
</tbody>
</table>
文章
#
标题
如何使用EJS正确显示MongoDB“articles”集合中的文章?提前谢谢你的帮助 我怀疑您的控制器方法遇到错误,但当您收到错误时,您不会返回。我的第一个猜测是文章对文章,但我可能错了。更改此选项以帮助您了解情况:
list:function(req, res){
Articles.find({}).exec(function(err, articles){
if(err){
sails.log('Error summary: ' + err);
sails.log(err);
return res.send(500, {error: 'Database Error'}); // note the return!
}
res.view('list', {articles:articles});
});
},
否则,在错误跟踪方面做得不错 我怀疑您的控制器方法遇到错误,但当您收到错误时,您不会返回。我的第一个猜测是文章对文章,但我可能错了。更改此选项以帮助您了解情况:
list:function(req, res){
Articles.find({}).exec(function(err, articles){
if(err){
sails.log('Error summary: ' + err);
sails.log(err);
return res.send(500, {error: 'Database Error'}); // note the return!
}
res.view('list', {articles:articles});
});
},
否则,在错误跟踪方面做得不错 所以我反复遇到这个错误,我的解决办法是检查“articles”的定义。 如果您的ejs文件在使用jquery函数更新模型后正在访问模型“articles”,则在呈现ejs文件时,可以将更新后的“articles”模型作为参数传递。 像 这部分代码可以在控制器或app.js文件中指定(无论在何处渲染EJB)。
这样,在呈现ejs文件时,不会出现“无法读取未定义的属性'forEach'或“文章数据未定义”的错误。因此,我反复遇到此错误,我的修复方法是检查“文章”的定义。 如果您的ejs文件在使用jquery函数更新模型后正在访问模型“articles”,则在呈现ejs文件时,可以将更新后的“articles”模型作为参数传递。 像 这部分代码可以在控制器或app.js文件中指定(无论在何处渲染EJB)。 这样,在呈现ejs文件时,不会出现“无法读取未定义的属性'forEach'或“文章数据未定义”的错误