我正在使用nodejs尝试使用musql2和sequelize npm模块连接数据库中的两个表。 我有两个表的文本和类别。当我使用sequelize方法category.getText()时,text是category的子级,它返回错误“ createtext不是函数。
这是文本的模型
const Sequelize = require('sequelize)
const sequelize = require('../database/sequelize')
const Text = sequelize.define('Text', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
anyText: {
type: Sequelize.TEXT,
allowNull: false,
},
state: {
type: Sequelize.STRING,
allowNull: false,
},
})
module.exports = Text;
类别模型
const Sequelize = require('sequelize)
const sequelize = require('../database/sequelize')
const Category = sequelize.define('category', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
category: {
type: Sequelize.STRING,
allowNull: false,
},
})
module.exports = Category;
我要运行的代码
const sequelize = require('./database/sequelize')
const Text = require('./models/text')
const Category = require('./models/category')
Category.getTexts()
.then( text => {
console.log(text)
})
.catch(err => {
console.log(err)
})
Text.belongsTo(Category, { constraints: true, onDelete: 'CASCADE'});
Category.hasMany(Text),
//{ force: true}
sequelize
.sync()
// .sync({ force: true})
.then(result => {
console.log('connected');
})
.catch(err => {
console.log(err);
});
错误是
Category.getTexts()
^
TypeError: Category.getTexts is not a function
at Object.<anonymous> (C:\Users\karim\Desktop\ex\exp.js:6:10)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
答案 0 :(得分:0)
首先,您需要在模型旁边更改文本模型,例如addcategory关系。
const Sequelize = require('sequelize)
const sequelize = require('../database/sequelize')
const Category = require('./category.model');
const Text = sequelize.define('Text', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
anyText: {
type: Sequelize.TEXT,
allowNull: false,
},
state: {
type: Sequelize.STRING,
allowNull: false,
},
})
Text.belongsTo(Category, { constraints: true, onDelete: 'CASCADE'});
Category.hasMany(Text),
module.exports = Text;
此后,您必须以此更改运行代码。
const sequelize = require('./database/sequelize')
const Text = require('./models/text')
const Category = require('./models/category')
sequelize.sync().then(result => {
console.log('connected');
}).catch(err => {
console.log(err);
});
model.Category.findAll({
where:{ your condition },
include :[
{
model : model.Text,
attributes : [ text field name], // If want to get all the field simple delete this line
required:true, // If you want to find all the category which has text then pass true else false
},
]
})
.then(PostData => {
if(PostData.count > 0){
console.log("Post Data===>",PostData)
}
else{
console.log("Post Data===> No record found")
}
}).catch(err => {
console.log("Post Data===> No record found")
})