0

I have learned NodeJS but I had some problems with _getProductsFromFile function in the Product class, I think the problem is static reserved word and the error i got was

TypeError: this._getProductsFromFile is not a function at Function.fetchAll (/Users/huynhhuukhanh/HKhansh/BE/Learn-NodeJS/refactoring-MVC-pattern/models/product.js:57:10) at exports.getProduct (/Users/huynhhuukhanh/HKhansh/BE/Learn-NodeJS/refactoring-MVC-pattern/controllers/products.js:20:11) at Layer.handle [as handle_request] (/Users/huynhhuukhanh/HKhansh/BE/Learn-NodeJS/node_modules/express/lib/router/layer.js:95:5) at next (/Users/huynhhuukhanh/HKhansh/BE/Learn-NodeJS/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/Users/huynhhuukhanh/HKhansh/BE/Learn-NodeJS/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/Users/huynhhuukhanh/HKhansh/BE/Learn-NodeJS/node_modules/express/lib/router/layer.js:95:5) at /Users/huynhhuukhanh/HKhansh/BE/Learn-NodeJS/node_modules/express/lib/router/index.js:281:22 at Function.process_params (/Users/huynhhuukhanh/HKhansh/BE/Learn-NodeJS/node_modules/express/lib/router/index.js:335:12) at next (/Users/huynhhuukhanh/HKhansh/BE/Learn-NodeJS/node_modules/express/lib/router/index.js:275:10) at Function.handle (/Users/huynhhuukhanh/HKhansh/BE/Learn-NodeJS/node_modules/express/lib/router/index.js:174:3)

Code in models/product.js

odule.exports = class Product {
  constructor(title) {
    this.title = title;
  }

  save() {
    this._getProductsFromFile((products) => {
      products.push(this);
      fs.writeFile(
        path.join(
          path.dirname(process.mainModule.filename),
          "data",
          "products.json"
        ),
        JSON.stringify(products),
        (err) => {}
      );
    });
  }

  static fetchAll(cb) {
    this._getProductsFromFile(cb);
  }

  _getProductsFromFile(cb) {
    fs.readFile(
      path.join(
        path.dirname(process.mainModule.filename),
        "data",
        "products.json"
      ),
      (err, fileContent) => {
        if (err) cb([]);
        else cb(JSON.parse(fileContent));
      }
    );
  }
};

Code in controllers/products.js

const Product = require("../models/product");

exports.postAddProduct = (req, res, next) => {
  const product = new Product(req.body.title);
  product.save();
  res.redirect("/");
};

exports.getProduct = (req, res, next) => {
  Product.fetchAll((products) => {
    res.render("shop", {
      prods: products,
      pageTitle: "Shop",
      path: "/",
      hasProducts: products.length > 0,
      activeShop: true,
      productCSS: true,
    });
  });
};

I need some help, please Thanks all.

hkhansh
  • 9
  • 3
  • 1
    In anything `static`, `this` refers to the class, not the instance. Your class doesn’t have a static `_getProductsFromFile` defined. See [How does the “this” keyword work?](/q/3127429/4642212). – Sebastian Simon Jul 08 '21 at 14:43
  • 2
    [Duplicate](//google.com/search?q=site%3Astackoverflow.com+js+what+is+this+in+static) of [ES6: this within static method](/q/50285240/4642212). – Sebastian Simon Jul 08 '21 at 14:45
  • The problem is that one of your functions is static and the other one is not. Try adding static to your function "static _getProductsFromFile(cb)". – Ariel Jul 08 '21 at 14:49
  • @ArielAlvarado i'm also try this but it doesn't work – hkhansh Jul 08 '21 at 15:21
  • @hkhansh Don’t just try random things; try to understand them instead — it’s easy: `class A{ instanceMethod(){ console.log(this, "refers to the class instance."); static staticMethod(){ console.log(this, "refers to the class itself."); } }; (new A).instanceMethod(); A.staticMethod();`. You can only call instance methods on your instance, and you can only call static methods on your class. Read the [documentation](//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes). – Sebastian Simon Jul 08 '21 at 16:42

0 Answers0