Express.js 和 Sequelize:没有定义模型
Express.js and Sequelize: does not define models
我正在使用 express.js 和 sequelize 开发小型应用程序。
现在我有一个问题。我选择 this tutorial 并卡住了。
我得到
TypeError: Cannot read property 'findAll' of undefined
为
router.get('/news', function (req, res, next) {
models.Article.findAll().then(function(article) {
res.json(article);
});
});
我的模特:
'use strict';
module.exports = function(sequelize, DataTypes) {
var Article = sequelize.define('Article', {
title: DataTypes.STRING,
content: DataTypes.TEXT
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
return Article;
};
我通过 github 托管项目,您可以在那里看到任何代码。
感谢您的帮助。
更新: 添加 console.log(models) 到路由 /news。
可能与命名约定有关,因为Sequelize在模型名称中添加了复数形式。好的做法是在您的模型定义中使用复数名称,这样您就不会在稍后的代码中被它欺骗。
尝试在您的代码和模型声明中将 Article 更改为 Articles。
错误在 models/index.js
中。那里有:
...
.filter(function(file) {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
...
这排除了所有以 .js
结尾的文件。把最后的条件改成file.slice(-3) === '.js'
,意思就是不过滤掉article.js。文章定义在article.js.
为了调试它,我在 GET /news
的路由器功能中添加了以下行:
console.log('KEYS TO models: ' + JSON.stringify(Object.keys(models)));
这告诉我模型的唯一成员是 sequelize 和 Sequelize:
调试后发现defaultmodels/index.js有错误。
当它遍历模型文件夹中的文件时,过滤器
中有一个条件
(file.slice(-3) !== '.js')
如果文件 具有 .js 扩展名,这将 return 错误,这是不正确的。
将其修复为
(file.slice(-3) === '.js')
它会起作用的。祝你好运!
完全正确的构造:
fs
.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(function(file) {
var model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});
我正在使用 express.js 和 sequelize 开发小型应用程序。 现在我有一个问题。我选择 this tutorial 并卡住了。
我得到
TypeError: Cannot read property 'findAll' of undefined
为
router.get('/news', function (req, res, next) {
models.Article.findAll().then(function(article) {
res.json(article);
});
});
我的模特:
'use strict';
module.exports = function(sequelize, DataTypes) {
var Article = sequelize.define('Article', {
title: DataTypes.STRING,
content: DataTypes.TEXT
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
return Article;
};
我通过 github 托管项目,您可以在那里看到任何代码。
感谢您的帮助。
更新: 添加 console.log(models) 到路由 /news。
可能与命名约定有关,因为Sequelize在模型名称中添加了复数形式。好的做法是在您的模型定义中使用复数名称,这样您就不会在稍后的代码中被它欺骗。
尝试在您的代码和模型声明中将 Article 更改为 Articles。
错误在 models/index.js
中。那里有:
...
.filter(function(file) {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
...
这排除了所有以 .js
结尾的文件。把最后的条件改成file.slice(-3) === '.js'
,意思就是不过滤掉article.js。文章定义在article.js.
为了调试它,我在 GET /news
的路由器功能中添加了以下行:
console.log('KEYS TO models: ' + JSON.stringify(Object.keys(models)));
这告诉我模型的唯一成员是 sequelize 和 Sequelize:
调试后发现defaultmodels/index.js有错误。 当它遍历模型文件夹中的文件时,过滤器
中有一个条件(file.slice(-3) !== '.js')
如果文件 具有 .js 扩展名,这将 return 错误,这是不正确的。 将其修复为
(file.slice(-3) === '.js')
它会起作用的。祝你好运!
完全正确的构造:
fs
.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(function(file) {
var model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});