使用sequelize和express进行构建时出现未定义的错误

时间:2016-02-04 20:37:51

标签: mysql node.js express sequelize.js

我正在从表单中检索数据。在帖子上,当我想用​​sequilize进行构建时,我收到错误:

  

TypeError:无法读取属性' build'未定义的

index.js

router.post('/register', function(req, res){
  var name = req.body.name;
  var lastName = req.body.lastName;
  var email = req.body.email;
  var cellNumber = req.body.cellNumber;
  var password = req.body.password;
    console.log(req.body.lastName);
  models.User.build({name: name, lastName: lastName, email: email, cellNumber: cellNumber, password: password})
      .save();
});

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 || "test";
var config    = require(path.join(__dirname, '..', 'config', 'config.json'))[env];
var db        = {};
var sequelize = new Sequelize(config.database, config.username, config.password, config);

  sequelize.authenticate().then(function(){
    console.log('Database has been successfully authenticated');
    return true;
  }).catch(function(err){
    console.log('Failed to authenticate database: ', err);
    return false;
  });

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,
    cellNumber: DataType.INTEGER
  });
  return User;
};

WWW

#!/usr/bin/env mode
var debug = require('debug')('entreeapp')
var app = require('../server');
var models = require('../model');

app.set('port', process.env.PORT || 3000);

//server running
models.sequelize.sync({
    force: true
}).then(function () {
    var server = app.listen(app.get('port'), function(){
        console.log('The magic is happening on port '+server.address().port);
    });
});

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:4)

... sequelize.define('user', { .. 

应该是:

... sequelize.define('User', { ..