인프런 커뮤니티 질문&답변

leewj4242님의 프로필 이미지
leewj4242

작성한 질문수

Node.js 교과서 - 기본부터 프로젝트 실습까지

시퀄라이즈 관계 설정하기

오류가 나서 질문 드립니다.

작성

·

1.2K

0

app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var logger = require('morgan');

var indexRouter = require('./routes');
var usersRouter = require('./routes/users');
var { sequelize } = require('./models');

var app = express();
sequelize.sync();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

user.js

module.exports = (sequelize, DataTypes) => {
    return sequelize.define('user', {
        name : {
            type : DataTypes.STRING(20),
            allowNull : false,
            unique : true,
        },
        age : {
            type : DataTypes.INTEGER.UNSIGNED,
            allowNull : false,
            defaultValue : 0,
        },
        married : {
            type : DataTypes.BOOLEAN,
            allowNull : false,
        },
        comment : {
            type : DataTypes.TEXT,
            allowNull : true,
        },
        created_at : {
            type : DataTypes.DATE,
            allowNull : false,
            defaultValue : sequelize.literal('now()'),
        }
    }, {
        timestamps : false,
        underscored : true,
    });
}

index.js

const path = require('path');
const Sequelize = require('sequelize');

const env = process.env.NODE_ENV || 'development';
const config = require('../config/config.json')[env];

const sequelize = new Sequelize(config.database, config.username, config.password, config);

const db = {};

db.Sequelize = Sequelize;
db.sequelize = sequelize;

db.User = require('./user')(sequelize, Sequelize);
db.Comment = require('./comment')(sequelize, Sequelize);

db.User.hasMany(db.Comment, { foreignKey : 'commenter', sourceKey : 'id' });
db.Comment.belongsTo(db.user, { foreignKey : 'commenter', targetKey :  'id' });

module.exports = db;

comment.js

module.exports = (sequelize, DataTypes) => {
    return sequelize.define('comment', {
        comment : {
            type : DataTypes.STRING(100),
            allowNull : false,
        },
        created_at : {
            type : DataTypes.DATE,
            allowNull : false,
            defaultValue : sequelize.literal('now()')
        },
    }, {
        timestamps : false,
        underscored : true,
    });
}

이건 에러코드 입니다.

> learn-sequelize@0.0.0 start C:\Users\DELL\Desktop\JS연습\learn-sequelize

> node ./bin/www

C:\Users\DELL\Desktop\JS연습\learn-sequelize\node_modules\sequelize\lib\associations\mixin.js:93

      throw new Error(`${source.name}.${_.lowerFirst(Type.name)} called with something that's not a subclass of Sequelize.Model`);

      ^

Error: comment.belongsTo called with something that's not a subclass of Sequelize.Model

    at Function.<anonymous> (C:\Users\DELL\Desktop\JS연습\learn-sequelize\node_modules\sequelize\lib\associations\mixin.js:93:13)

    at Object.<anonymous> (C:\Users\DELL\Desktop\JS연습\learn-sequelize\models\index.js:18:12)

    at Module._compile (internal/modules/cjs/loader.js:955:30)

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)

    at Module.load (internal/modules/cjs/loader.js:811:32)

    at Function.Module._load (internal/modules/cjs/loader.js:723:14)

    at Module.require (internal/modules/cjs/loader.js:848:19)

    at require (internal/modules/cjs/helpers.js:74:18)

    at Object.<anonymous> (C:\Users\DELL\Desktop\JS연습\learn-sequelize\app.js:8:21)

    at Module._compile (internal/modules/cjs/loader.js:955:30)

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)

    at Module.load (internal/modules/cjs/loader.js:811:32)

    at Function.Module._load (internal/modules/cjs/loader.js:723:14)

    at Module.require (internal/modules/cjs/loader.js:848:19)

    at require (internal/modules/cjs/helpers.js:74:18)

    at Object.<anonymous> (C:\Users\DELL\Desktop\JS연습\learn-sequelize\bin\www:7:11)

    at Module._compile (internal/modules/cjs/loader.js:955:30)

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)

    at Module.load (internal/modules/cjs/loader.js:811:32)

    at Function.Module._load (internal/modules/cjs/loader.js:723:14)

    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)

    at internal/main/run_main_module.js:17:11

npm ERR! code ELIFECYCLE

npm ERR! errno 1

npm ERR! learn-sequelize@0.0.0 start: `node ./bin/www`

npm ERR! Exit status 1

npm ERR!

npm ERR! Failed at the learn-sequelize@0.0.0 start script.

npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:

npm ERR!     C:\Users\DELL\AppData\Roaming\npm-cache\_logs\2020-05-29T11_34_06_481Z-debug.log

답변 2

1

제로초(조현영)님의 프로필 이미지
제로초(조현영)
지식공유자

db.User 대문자입니다.

0

leewj4242님의 프로필 이미지
leewj4242
질문자

감사합니다.

leewj4242님의 프로필 이미지
leewj4242

작성한 질문수

질문하기