【问题标题】:Sails, EJS data is not defined in forEach loopSails,EJS 数据未在 forEach 循环中定义
【发布时间】:2020-08-29 17:58:20
【问题描述】:

我正在使用基于 @bradtraversy 教程的 MongoDB 开发 Sails.js CRUD 应用程序(文章管理)。我在尝试使用 EJS 语法和 forEach 循环显示数据库中的文章时遇到了问题。错误提示“未定义文章数据”。 Error image

基本上我通过 Sails /Create?title=ArticleOne%20body=BodyOne URL 蓝图将示例记录传递给 Mongo。在Mongo中可以清楚地看到该文章。 MongoDB article image

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' },

};

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 视图:

<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“文章”集合的文章?提前感谢您的帮助!

【问题讨论】:

    标签: mongodb express sails.js ejs sails-mongo


    【解决方案1】:

    我怀疑您的控制器方法遇到错误,但是当您收到错误时您没有返回。我的第一个猜测是 Article vs 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});
        });
    },
    

    否则,在错误跟踪方面做得很好!

    【讨论】:

      【解决方案2】:

      所以我反复收到此错误,我的解决方法是检查“文章”的定义。 如果您的 ejs 文件在使用 jquery 函数更新模型后访问模型“文章”,那么当您渲染 ejs 文件时,您可以将更新后的“文章”模型作为参数传递。 喜欢,

          const articles = await Article.find().sort('-entryTime');
          res.render('route/ejsfilename', { articles:articles });
      

      这部分代码可以在你的控制器或 app.js 文件中指定(无论你在哪里渲染你的 ejs)。 这样在渲染ejs文件时,就不会出现“Cannot read property 'forEach' of undefined”或“articles data is not defined”的错误。

      【讨论】:

        【解决方案3】:

        转到 config/blueprints.js,设置 动作:真

        蓝图会为你自动路由。

        【讨论】:

          猜你喜欢
          • 2019-01-02
          • 1970-01-01
          • 2017-09-29
          • 2020-01-21
          • 1970-01-01
          • 2018-11-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多