I'm having trouble taking the data from the table and creating an API. The table isn't empty but req.body is and it says Vehicle is undefined ? The exact Error message I get is "TypeError: Cannot read property 'findAll' of undefined" and the req.body response is an empty object.
Please help, I've tried everything.
Model:
import { Model } from 'sequelize'; const PROTECTED_ATTRIBUTES = ['password']; export default (sequelize, DataTypes) => { class Vehicles extends Model { toJSON() { // hide protected fields const attributes = { ...this.get() }; // eslint-disable-next-line no-restricted-syntax for (const a of PROTECTED_ATTRIBUTES) { delete attributes[a]; } return attributes; } /** * 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(vehicles) { // define association here } }; Vehicles.init({ // id: DataTypes.INTEGER, make: DataTypes.STRING, model: DataTypes.STRING, createdAt: DataTypes.DATE, updatedAt: DataTypes.DATE }, { sequelize, modelName: 'Vehicles', }); return Vehicles; };
Models/index.js:
import fs from 'fs'; import path from 'path'; import Sequelize from 'sequelize'; import enVariables from '../config/config.json'; const basename = path.basename(__filename); const env = process.env.NODE_ENV || 'development'; const config = enVariables[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 => (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js')) .forEach(file => { // eslint-disable-next-line global-require,import/no-dynamic-require const model = require(path.join(__dirname, file)).default(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; export default db;
Index.js:
const express = require('express'); const models = require('./src/models'); const Vehicles = models.Vehicles; const app = express(); var bodyParser = require('body-parser'); const jsonParser = bodyParser.json(); app.use(bodyParser.urlencoded({ extended: true })) app.use(bodyParser.json()) app.get('/', function(req, res){ res.send('Hello, please go to /vehicles !') }) app.get('/vehicles', jsonParser, async function(req ,res){ // res.send('page under construction') // console.log(Vehicles.findOne()) Vehicles.findAll().then(users => { return users }); }); const port = 5000; app.listen(port, () => { console.log('App is now running at port ', port) })
Issue has been solved, it is because I was using EcmaScript
modules ( export
, import
) in models/index.js
, then importing it using CommonJS
( module.exports
, require
) in index.js. That caused issues. I should have used the same module format on both sides, or console.log
what require('./src/models')
outputs to debug it.
I changed:
const models = require('./src/models')
To:
const models = require('./src/models').default;
but I ideally should only use one module format
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.