Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向数据库发送和处理请求的问题_Javascript_Node.js_Express_Sequelize.js - Fatal编程技术网

Javascript 向数据库发送和处理请求的问题

Javascript 向数据库发送和处理请求的问题,javascript,node.js,express,sequelize.js,Javascript,Node.js,Express,Sequelize.js,我用邮递员发送请求。当我发送创建产品的请求“POST”(或其他请求:GET、PUT等)时,我遇到了以下问题: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'create' of undefined 我不知道如何向我的数据库发送查询以获得正确的结果 我使用express、sequelize和sequelize cli、mysql2。我还有迁移文件 myserver.js const express = req

我用邮递员发送请求。当我发送创建产品的请求“POST”(或其他请求:GET、PUT等)时,我遇到了以下问题:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'create' of undefined
我不知道如何向我的数据库发送查询以获得正确的结果

我使用express、sequelize和sequelize cli、mysql2。我还有迁移文件

myserver.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;
将函数createProduct中的
productModels.js
更改为:

// Создаю в бд продукт
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);
    };
};
并对其他函数执行相同的操作

这是您的型号名称
products.js


“productController”似乎只是
productRouter
的一个副本,错误意味着没有
create()
函数,而实际上没有。您需要使用
模型
执行CRUD操作。错误检查中也有错误-响应永远不会等于
错误
,但可能是
错误实例
,或者最好使用
尝试/捕获
...
}, {
  sequelize,
  modelName: 'products',
});
return products;
...