我正在用nodejs制作一个应用程序,我想对我的模型进行连接,但是我仍然遇到相同的错误:
TypeError:user.findAll不是函数
routes / user.js
const express = require('express');
const router = express.Router();
const db = require('../config/database');
const user = require('../models/users');
router.get('/', (req, res) => user.findAll()
.then(users => {
console.log(users)
res.sendStatus(200);
})
.catch(err => console.log(err)));
module.exports = router;
models / user.js
'use strict';
const Sequelize = require('sequelize');
const db = require('../config/database')
module.exports = (sequelize, DataTypes) => {
const users = sequelize.define('users', {
id: DataTypes.INTEGER,
name: DataTypes.STRING,
age: DataTypes.INTEGER,
email: DataTypes.STRING,
country: DataTypes.STRING,
state: DataTypes.STRING,
city: DataTypes.STRING
}, {});
users.associate = function(models) {
// associations can be defined here
};
return users;
};
答案 0 :(得分:1)
您应该使用db.users.findAll()
而不是user.findAll()
。它会工作
答案 1 :(得分:0)
我发现了错误,我无法正确导入/导出,我将我的models / user.js文件更改为:
'use strict';
const Sequelize = require('sequelize');
const db = require ('../config/database')
const DataTypes = require('sequelize/lib/data-types')
const User = db.define('users', {
name: DataTypes.STRING,
age: DataTypes.INTEGER,
email: DataTypes.STRING,
country: DataTypes.STRING,
state: DataTypes.STRING,
city: DataTypes.STRING
}, {});
User.associate = function(models) {
// associations can be defined here
};
module.exports = User