Cannot get model name for Sequelize using mysql

I am new to nodejs and the Sequelize ORM framework. I am trying to get it to work with mysql. I have achieved great success, but at the moment I’m stuck in the part where you need to load the sequel in the models. But I get an error when the name is retrieved from the file.

WWW

var debug = require('debug')('sampleapp') var app = require('../server'); var models = require('../model'); app.set('port', process.env.PORT || 3000); //server running models.sequelize.sync().then(function () { var server = app.listen(app.get('port'), function(){ debug('The magic is happening on port '+server.address().port); }); }); 

index.js

 "use strict" var fs = require('fs'); var path = require('path'); var Sequelize = require('sequelize'); var debug = require('debug'); var env = process.env.NODE_ENV || "development"; var config = require(path.join(__dirname, '..', 'config', 'config.json'))[env]; var sequelize = new Sequelize(config.database, config.username, config.password, config); var db = {}; fs.readdirSync(__dirname) .filter(function(file) { return (file.indexOf(".") !== 0) && (file !== 'index.js') }) .forEach(function(file) { var model = sequelize['import'](path.join(__dirname, file)) db[model.name] = model }); db.sequelize = sequelize; db.Sequelize = Sequelize; module.exports = db; 

user.js

 module.exports = function(sequelize, DataType){ var User = sequelize.define('user', { name: DataType.STRING, password: DataType.STRING, lastName: DataType.STRING, email: DataType.STRING, gender: DataType.CHAR, cellNumber: DataType.INTEGER }, { instanceMethods : { create : function(onSuccess, onError){ var name = this.name; var lastName = this.lastName; var email = this.email; var gender = this.gender; var cellNumber = this.cellNumber; var password = this.password; var shasum = crypto.createHash('sha1'); shasum.update(password); password = shasum.digest('hex'); User.build({name: name, lastName: lastName, email: email, gender: gender, cellNumber: cellNumber, password: password}) .save().success(onSuccess).error(onError); } } }); }; 

I keep getting this error in db [model.name] = model:

TypeError: cannot read property 'name' from undefined

Some time ago there was this error. any help would be greatly appreciated

+5
source share
1 answer

It is likely that you will take a non-model file in the model directory. What other files do you have in this directory? You want to import only javascript files, and the file filter is not specific enough. Try to make it detect only javascript files. A simple way is to check the last three characters of the file name and make sure they are .js .

 (file.slice(-3) === '.js') 

Add it to the file filter as follows:

 .filter(function(file) { return (file.indexOf('.') !== 0) && (file !== "index.js") && (file.slice(-3) === '.js'); }) 

Another reason this may fail is because the sequelize instance is not successfully created and does not work before you can import the models. I use something like this to make sure I have a connection before importing:

 var sequelize = new Sequelize(foo, bar, baz); sequelize.authenticate().then(function() { console.log('Database connected and authenticated!'); return true; }).catch(function(err) { console.error('Failed to connect and authenticate', err); return false; }); 

If you used something like this, it would be easier to detect errors. Try putting a database check on existing code (or even just temporarily comment on your code) and confirm that you are really connecting successfully. Once you can confirm that you are connected, it is much easier to debug strange things.

Note. Do not leave the database check there as soon as you confirm its work. This is a Promise and doesn't work very well with synchronous functions like fs.readdirSync() .

+3
source

Source: https://habr.com/ru/post/1242215/


All Articles