我正在使用 Sequelize,一切看起来都很好,直到邮递员提出请求。控制台显示错误
类型错误:db.Invoicer.find 不是函数
这是我的代码
发票.model.js
const { DataTypes } = require("sequelize");
module.exports = model;
function model(sequelize) {
const attributes = {
id:{type: DataTypes.STRING(50),
allowNull: false,
primaryKey: true,},
clientname:{type: DataTypes.STRING(3999), allowNull: true},
clientaddress:{type: DataTypes.STRING(3999), allowNull: true},
clientdistrict:{type: DataTypes.STRING(3999), allowNull: true},
clientcity:{type: DataTypes.STRING(3999), allowNull: true},
clientprovince:{type: DataTypes.STRING(3999), allowNull: true},
clientcountry:{type: DataTypes.STRING(3999), allowNull: true},
clientponumber:{type: DataTypes.STRING(3999), allowNull: true},
shipname:{type: DataTypes.STRING(3999), allowNull: true},
shipaddress:{type: DataTypes.STRING(3999), allowNull: true},
shipdistrict:{type: DataTypes.STRING(3999), allowNull: true},
shipcity:{type: DataTypes.STRING(3999), allowNull: true},
shipprovince:{type: DataTypes.STRING(3999), allowNull: true},
shipcountry:{type: DataTypes.STRING(3999), allowNull: true},
shipponumber:{type: DataTypes.STRING(3999), allowNull: true},
customerpo:{type: DataTypes.STRING(3999), allowNull: true},
shipvia:{type: DataTypes.STRING(3999), allowNull: true},
shipdate:{type: DataTypes.STRING(3999), allowNull: true},
termofpayment:{type: DataTypes.STRING(3999), allowNull: true},
duedate:{type: DataTypes.STRING(3999), allowNull: true},
invoicedate:{type: DataTypes.STRING(3999), allowNull: true},
approvedby:{type: DataTypes.STRING(3999), allowNull: true},
clientsignature:{type: DataTypes.STRING(3999), allowNull: true},
additionalnotes:{type: DataTypes.STRING(3999), allowNull: true},
};
const options = {
freezeTableName: true,
// don't add the timestamp attributes (updatedAt, createdAt)
timestamps: false,
};
return sequelize.define("Invoicer", attributes, options);
}
items.model.js
const { DataTypes } = require("sequelize");
module.exports = model;
function model(sequelize) {
const attributes = {
id:{
type: DataTypes.STRING(3999),
allowNull: false,
primaryKey: true,
},
invoiceid:{type: DataTypes.STRING(50), allowNull: true},
currency:{type: DataTypes.STRING(3999), allowNull: true},
itemdesc:{type: DataTypes.STRING(3999), allowNull: true},
quantity:{type: DataTypes.STRING(3999), allowNull: true},
unit:{type: DataTypes.STRING(3999), allowNull: true},
price:{type: DataTypes.STRING(3999), allowNull: true},
discount:{type: DataTypes.STRING(3999), allowNull: true},
freightprice:{type: DataTypes.STRING(3999), allowNull: true},
taxamount:{type: DataTypes.STRING(3999), allowNull: true},
downpayment:{type: DataTypes.STRING(3999), allowNull: true},
};
const options = {
freezeTableName: true,
// don't add the timestamp attributes (updatedAt, createdAt)
timestamps: false,
};
return sequelize.define("Items", attributes, options);
}
db.js
const { Sequelize } = require("sequelize");
const invoicerModel = require("../models/invoicer.model");
const itemsModel = require("../models/items.model");
require("dotenv").config();
const sequelize = new Sequelize(
process.env.DB,
process.env.USER,
process.env.PASSWORD,
{
host: process.env.HOST,
port: process.env.SQL_PORT,
dialect: process.env.DIALECT,
dialectOptions: {
options: { encrypt: false },
},
}
);
const db = {};
db.Invoicer = invoicerModel(sequelize).hasMany(itemsModel(sequelize),{foreignKey:'invoiceid'});
db.Items = itemsModel(sequelize).belongsTo(invoicerModel(sequelize),{foreignKey:'id'});
sequelize.sync({ alter: true });
module.exports = db;
发票.service.js
const db = require("../config/db");
const getAll = async () => {
return await db.Invoicer.findAll();
};
const findInvoiceById = async (id) => {
return await db.Invoicer.findByPk(id);
};
const deleteInvoice = async (Id) => {
await db.Invoicer.destroy({
where: { Id: Id },
});
};
module.exports = {
getAll,
findInvoiceById,
deleteInvoice,
}
发票.controller.js
const express = require("express");
const router = express.Router();
const InvoicerService = require("../services/invoicer.service");
router.get("/", async (req, res) => {
try {
var invoice = await InvoicerService.getAll();
res.json(invoice);
} catch (error) {
console.log(error);
res.status(500).json({ statusCode: 500, error: "Something went wrong" });
}
});
router.get("/:id", async (req, res) => {
try {
var invoice = await InvoicerService.findInvoiceById(req.params.id);
if (!invoice) {
return res
.status(404)
.json({ statusCode: 404, error: "Invoice Does not exist" });
}
return res.json(invoice);
} catch (error) {
return res
.statusCode(500)
.json({ statusCode: 500, error: "Something went wrong" });
}
});
router.delete("/:id", async (req, res) => {
try {
var exisitingInvoice = await InvoicerService.findInvoiceById(req.params.id);
if (!exisitingInvoice) {
return res
.status(404)
.json({ statusCode: 404, error: "Invoice Does not exist" });
}
await InvoicerService.deleteInvoice(req.params.id);
return res.json({
statusCode: 200,
message: `invoice with id: ${req.params.id} is deleted successfully`,
});
} catch (error) {
return res
.statusCode(500)
.json({ statusCode: 500, error: "Something went wrong" });
}
});
module.exports = router;
我想将项目表连接到发票表,项目表中的外键是发票 ID 链接到发票表中的 id 列
关联函数返回关联对象而不是模型本身。单独调用它们就可以了:
const invoicer = invoicerModel(sequelize)
const items = itemsModel(sequelize)
invoicer.hasMany(items, { foreignKey:'invoiceid' });
db.Invoicer = invoicer;
// please pay attention that you need to indicate the same foreignKey value in both associations
items.belongsTo(invoicer, { foreignKey:'invoiceid' })
db.Items = items;