【问题标题】:Node.js Sequelize Cannot read Property foreach of nulllNode.js Sequelize 无法读取 null 的属性 foreach
【发布时间】:2018-04-29 17:57:01
【问题描述】:

这是错误 TypeError: C:\users\gaffer\desktop\6th Semester\GafferCart\views\admin\add_product.ejs:22

执行(默认):SELECT count(*) AS count FROM Products AS Product; 执行(默认):SELECT id, title, slug, desc, category, price, images FROM Products AS Product LIMIT 1; 执行(默认):SELECT id, title, slug FROM Categories AS Category LIMIT 1;

    20|         <label for="">Category</label>

   21|         <select name="category" class="form-control">

 >> 22|             <% categories.forEach(function(cat){ %>

    23|                 <option value="<%= cat.slug %>"><%= cat.title %
</option>
    24|             <% }); %>
    25|         </select

>

无法读取 null 的属性“forEach”

          // Get Product model
          var Product = require('../models/product');

        // Get Product model
        var Category = require('../models/category');

            /*
            * Get products index 
            */

            router.get('/',function(req,res){

                var count;

                models.Product.count(function(c){
                    count=c;
                })
                //models.product.find
                models.Product.find({
           attributes: 
                    ['id','title','slug','desc','category','price','images']
                })
                .then(function(products){
                    res.render('admin/products',{
                        products:products,
                        count:count
                    });
                });
            });

                /*
                * GET add product
                */
    router.get('/add-product', function (req, res) {

        var title = "";
        var desc = "";
        var price = "";

        models.Category.find({
            attributes:['id','title','slug']
        })   
        //.then((categories)=>{   both are giving same errors

           .then(function(categories){
            res.render('admin/add_product', {
                title: title,
                desc: desc,
                categories: categories,
                price: price
            });

        });
 });

【问题讨论】:

标签: node.js express sequelize.js


【解决方案1】:

您的类别对象为空,请控制该对象。

【讨论】:

  • 我刚刚渲染了 res.json(categories);
  • {"id":1,"title":"asad","slug":"asad"} 它给了我这个答案
  • 这些所有字段都来自 Products 请 Noman Maqsood 检查我的第二个 Get add Products res.json(categories); // res.render('admin/add_product', { // title: title, // desc: desc, // categories: categories, // price: price // });
猜你喜欢
  • 2023-03-21
  • 2015-05-31
  • 1970-01-01
  • 1970-01-01
  • 2020-02-16
  • 2023-03-22
  • 2022-11-24
  • 2018-05-13
  • 2017-03-25
相关资源
最近更新 更多