Skip to content
Advertisement

The problem of sending and processing a request to the database

I send requests using postman. And when I send request “POST” for create product (or another requests: GET, PUT , etc), I have problem:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'create' of undefined

I can’t figure out how I can send a query to my database to get this right.

I use express, sequelize and sequelize-cli, mysql2. And also i have migrations file.

My server.js

const express = require("express");
const productRouter = require("./routers/productRouter");
const discountRouter = require("./routers/discountRouter")

const app = express();
const PORT = process.env.PORT || 3000;

app.use(express.json());

// Запрос на таблицу с продуктами
app.use("/shop", productRouter);
// Запрос на таблицу со скидками
app.use("/shop", discountRouter);

app.listen(PORT, () => console.log("Server is working ... "));

productRouter.js

const Router = require("express");
const router = new Router();
const productController = require("../controllers/productControllers");

// Получаем все товары
router.get("/products", async (req, res, next) => {
    const resultOfGetAllProducts = await productController.all();
    if(resultOfGetAllProducts === Error) {
        return res.sendStatus(500).json(Error);
    } else {
        return res.sendStatus(200).json(resultOfGetAllProducts);
    };
});

// Получаем конкретный товар
router.get("/product/:id", async (req, res, next) => {
    if(!req.params.id) return res.sendStatus(400).send("Product ID is not specified.");
    const id = req.params.id;
    const result = null;
    const resultOfGetOneProduct = await productController.one(id, result);
    if(resultOfGetOneProduct === Error) {
        return res.sendStatus(500).json(Error);
    } else {
        return res.sendStatus(200).json(resultOfGetOneProduct);
    };
});

// Добавляем товар
router.post("/product", async (req, res, next) => {
    if(!req.body) return res.sendStatus(400).send("Product parameters are not specified.");
    const product = {
        product_name: req.body.product_name,
        price: req.body.price,
        product_description: req.body.product_description
    };
    const result = null;
    const resultOfCreateProduct = await productController.create(product, result);
    if (resultOfCreateProduct === Error) {
        return res.sendStatus(500).json(Error);
    } else {
        return res.sendStatus(200).send("The product has been created.");
    }
});

// Обновляем товар
router.put("/product/:id", async (req, res, next) => {
    if(!req.params.id) return res.sendStatus(400).send("Product ID is not specified.");
    if(!req.body) return res.sendStatus(400).send("Product parameters are not specified.");
    const product = {
        product_name: req.body.product_name,
        price: req.body.price,
        product_description: req.body.product_description
    }
    const id = {id: req.params.id};
    const result = null;
    const resultOfUpdateProduct = await productController.update(id, product, result);
    if(resultOfUpdateProduct === Error) {
        return res.sendStatus(500).json(Error);
    } else {
        return res.sendStatus(200).send("The product has been updated.");
    };
});

// Удаляем товар
router.delete("/product/:id", async (req, res, next) => {
    if(!req.params.id) return res.sendStatus(400).send("Product ID is not specified.");
    const id = {id: req.params.id};
    const result = null;
    const resultOfDeleteProduct = await productController.delete(id, result);
    if(resultOfDeleteProduct === Error) {
        return res.sendStatus(500).json(Error);
    } else {
        return res.sendStatus(200).send("The product has been deleted.");
    }
});


module.exports = router;

productController.js

const Router = require("express");
const router = new Router();
const productController = require("../controllers/productControllers");

// Получаем все товары
router.get("/products", async (req, res, next) => {
    const resultOfGetAllProducts = await productController.all();
    if(resultOfGetAllProducts === Error) {
        return res.sendStatus(500).json(Error);
    } else {
        return res.sendStatus(200).json(resultOfGetAllProducts);
    };
});

// Получаем конкретный товар
router.get("/product/:id", async (req, res, next) => {
    if(!req.params.id) return res.sendStatus(400).send("Product ID is not specified.");
    const id = req.params.id;
    const result = null;
    const resultOfGetOneProduct = await productController.one(id, result);
    if(resultOfGetOneProduct === Error) {
        return res.sendStatus(500).json(Error);
    } else {
        return res.sendStatus(200).json(resultOfGetOneProduct);
    };
});

// Добавляем товар
router.post("/product", async (req, res, next) => {
    if(!req.body) return res.sendStatus(400).send("Product parameters are not specified.");
    const product = {
        product_name: req.body.product_name,
        price: req.body.price,
        product_description: req.body.product_description
    };
    const result = null;
    const resultOfCreateProduct = await productController.create(product, result);
    if (resultOfCreateProduct === Error) {
        return res.sendStatus(500).json(Error);
    } else {
        return res.sendStatus(200).send("The product has been created.");
    }
});

// Обновляем товар
router.put("/product/:id", async (req, res, next) => {
    if(!req.params.id) return res.sendStatus(400).send("Product ID is not specified.");
    if(!req.body) return res.sendStatus(400).send("Product parameters are not specified.");
    const product = {
        product_name: req.body.product_name,
        price: req.body.price,
        product_description: req.body.product_description
    }
    const id = {id: req.params.id};
    const result = null;
    const resultOfUpdateProduct = await productController.update(id, product, result);
    if(resultOfUpdateProduct === Error) {
        return res.sendStatus(500).json(Error);
    } else {
        return res.sendStatus(200).send("The product has been updated.");
    };
});

// Удаляем товар
router.delete("/product/:id", async (req, res, next) => {
    if(!req.params.id) return res.sendStatus(400).send("Product ID is not specified.");
    const id = {id: req.params.id};
    const result = null;
    const resultOfDeleteProduct = await productController.delete(id, result);
    if(resultOfDeleteProduct === Error) {
        return res.sendStatus(500).json(Error);
    } else {
        return res.sendStatus(200).send("The product has been deleted.");
    }
});


module.exports = router;

productModels.js

const Sequelize = require("sequelize");
const productsModel = require("../models/products.js");
const db = require("../models/index.js");

// Получаю с бд все продукты
exports.all = async function getProducts(cb) {
    //const products = await db.query('SELECT * FROM products');
    const products = await db.findAll({raw: true});
    if(products === null) {
        return cb(Error, null);
    } else {
        return cb(null, products);
    };
};

// Получаю с бд конкретный продукт
exports.one = async function getOneProduct(id, cb) {
    //const getOneQuery = 'SELECT * FROM product where id = $1';
    //const getDiscount = 'SELECT * FROM discounts where product_id = $1';
    //const curDiscount = await Discount.findOne({where: id});
    const product = await db.findAll({where: {id: id}});
    const productDiscount = await Discount.findAll({where: {product_id: id}});
    const priceWithDiscount = product.price - (product.price * ((productDiscount.discount)/100));
    if (product === null) {
        return cb(Error, null);
    } else if(productDiscount === null) {
        return cb(null, product)
    } else {
        product.price = priceWithDiscount;
        const result = product;
        return cb(null, result);
    };
};

// Создаю в бд продукт
exports.create = async function createProduct(product, cb) {
    // const createQuery = 'INSERT INTO product (product_name, price, product_description) values ($1, $2, $3) RETURNING *';
    // const arrayQuery = [product.product_name, product.price, product.product_description];
    // const newProduct = await db.query(createQuery, arrayQuery);    
    const newProduct = await db.productsModel.create(product);
    if(newProduct === null) {
        return cb(Error, null);
    } else {
        return cb(null, newProduct);
    };
};

// Обновляю в бд конкретный продукт
exports.update = async function updateProduct(id, newData, cb) {
    // const updateQuery = 'UPDATE product set product_name = $1, price = $2, product_description = $3 where id = $4 RETURNING *';
    // const arrayQuery = [newData.product_name, newData.price, newData.product_description, id];
    const product = await db.update(newData, {where: id});
    if(product === null) {
        return cb(Error, null);
    } else {
        return cb(null, product);
    };
};

// Удаляю в бд конкретный продукт
exports.delete = async function deleteProduct(id, cb) {
    //const deleteQuery = 'DELETE FROM product where id = $1';
    const product = await db.destroy({where: id});
    if(product === null) {
        return cb(Error, null);
    } else {
        return cb(null, product);
    };
};

product.js

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class products extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }

    static async addProduct (params) {
      await this.findOrCreate(
        {where: {
            product_name: params.product_name,
            price: params.price,
            product_description: params.product_description
        }});
    }
  };
  products.init({
    product_name: DataTypes.STRING,
    price: DataTypes.NUMBER,
    product_description: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'products',
  });
  return products;
};

index.js

'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

Advertisement

Answer

Change in productModels.js the function createProduct to:

// Создаю в бд продукт
exports.create = async function createProduct(product, cb) {    
    const newProduct = await db['products'].create(product); // db['modelName'], in your case is products, check your model
    // or, const newProduct = await db.products.create(product)
    if(newProduct === null) {
        return cb(new Error('Product empty'), null);
    } else {
        return cb(null, newProduct);
    };
};

And do the same to other functions.

Here your model name in products.js:

...
}, {
  sequelize,
  modelName: 'products',
});
return products;
...
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement