无法从控制器访问NodeJS模型类使用EJS

问题描述

请立即开始学习node.js并使用一些在线资源,并且需要一些帮助。我正在关注购物车的购物项目。安装程序使用MVC模式。我需要获取product.price才能填充购物车。但是出现TypeError:无法读取未定义的属性“ price”。

请在代码下方查找。

  1. 模型: product.js
    const fs = require('fs');
    const path = require('path');    
    const p = path.join(
    path.dirname(require.main.filename),'data','products.json');
    
    const getProductsFromFile = cb => {
      fs.readFile(p,(err,fileContent) => {
        if (err) {
          cb([]);
        } else {
          cb(JSON.parse(fileContent));
        }
      });
    };
    
    module.exports = class Product {
      constructor(title,imageUrl,description,price) {
        this.title = title;
        this.imageUrl = imageUrl;
        this.description = description;
        this.price = price;
      }
    
      save() {
        this.id = Math.random().toString();
        getProductsFromFile(products => {
          products.push(this);
          fs.writeFile(p,JSON.stringify(products),err => {
            console.log(err);
          });
        });
      }
    
      static fetchAll(cb) {
        getProductsFromFile(cb);
      }
    
      static findById(id,cb){
        getProductsFromFile(products => {
          const product = products.find(p => p.id === id);
          cb(product);
        });
      }
    };

cart.js

         const fs = require('fs');
         const path = require('path');
        
            const p = path.join(
              path.dirname(require.main.filename),'cart.json'
            );
            
            module.exports = class Cart {
              static addProduct(id,productPrice) {
                // Fetch the previous cart
                console.log(p)
                fs.readFile(p,fileContent) => {
                  let cart = { products: [],totalPrice: 0 };
                  if (!err) {
                    cart = JSON.parse(fileContent);
                  }
                  // Analyze the cart => Find existing product
                  const existingProductIndex = cart.products.findIndex(prod => prod.id === id);
                  const existingProduct = cart.products[existingProductIndex];
                  let updatedProduct;
                  // Add new product/ increase quantity
                  if (existingProduct) {
                    updatedProduct = { ...existingProduct };
                    updatedProduct.qty = updatedProduct.qty + 1;
                    cart.products = [...cart.products];
                    cart.products[existingProductIndex] = updatedProduct;
                  } else {
                    updatedProduct = { id: id,qty: 1 };
                    cart.products = [...cart.products,updatedProduct];
                  }
                  cart.totalPrice = cart.totalPrice + +productPrice;
                  fs.writeFile(p,JSON.stringify(cart),err => {
                    console.log(err);
                  });
                });
              }
            };

shop.js-控制器

    
    const Product = require('../models/product');
    const Cart = require('../models/cart');
    
    exports.postCart = (req,res,next) => {
      const prodId = req.body.productId;
      Product.findById(prodId,product => {
          //console.log(product);
        Cart.addProduct(prodId,product.price);
      });
      res.redirect('/cart');
    }; 

由于产品值为空,因此无法获得product.price。 任何帮助将不胜感激。在学习完本教程之后,就创建了产品实例,但是无法弄清楚为什么仍未定义产品。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...