NodeJS 未定义序列化-序列化并交付

ffvjumwh  于 9个月前  发布在  Node.js
关注(0)|答案(4)|浏览(137)

我是新来的。
我正试图在我的简单应用程序中添加序列化与cosign。
config/db.js

var Sequelize = require('sequelize');

var sequelize = new Sequelize('test', 'root', '', {

host: 'localhost',
dialect: 'mysql',

 pool: {
max: 5,
min: 0,
idle: 10000
}

});

module.exports = function () {
return sequelize
}

字符串
model/user.js

var Sequelize = require('sequelize');

module.exports = function(application, req, res){

var User = sequelize.define('user', {
username: {
type: Sequelize.STRING,
}
}, {
freezeTableName: true // Model tableName will be the same as the model name
});

User.create({ username: 'fnord'})
.then(function() {
console.log('criado!');
})
}


config/server.js

...
consign()
.include('app/routes')
.then('config/db.js')
.then('app/models')
.then('app/controllers')
.into(app);

module.exports = app;


我得到错误sequelize is not defined´ on var User = sequelize.define('user',{`
我做错了什么?

qnyhuwrf

qnyhuwrf1#

在moldes文件夹中创建一个index.js文件,如下所示:

"use strict";

var fs        = require("fs");
var path      = require("path");
var Sequelize = require("sequelize");

var sequelize = new Sequelize(global.config.dbConfig.name, global.config.dbConfig.user, global.config.dbConfig.password, {
  host: global.config.dbConfig.host,
  port: global.config.dbConfig.port,
  pool: false
});

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;
  });

Object.keys(db).forEach(function(modelName) {
  if ("associate" in db[modelName]) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;

module.exports = db;

字符串
然后在user.js中做这样的事情:

module.exports = function(sequelize, DataTypes) {
  var User = sequelize.define("User", {
    username: {
      type: DataTypes.STRING
    }, 
    {
      freezeTableName: true // Model tableName will be the same as the model name 
    }
  });

  return User;
}


http://docs.sequelizejs.com/en/1.7.0/articles/express/

esyap4oy

esyap4oy2#

您应该要求将示例序列化到用户模型中
config/db.js
第一个月
model/user.js

var Sequelize = require('sequelize');
var sequelize = require('../config/db.js'); //sequelize instance

module.exports = function(application, req, res){

var User = sequelize.define('user', { 
...

字符串

toe95027

toe950273#

Sequelize-CLI对于使用Sequelize的项目来说是一个非常有用的工具。

npm install -g sequelize-cli

字符串
然后可以运行

sequelize init


上面的命令会为你写出几个文件夹,包括一个models文件夹,里面有Ricardo在上面创建的索引文件。这也提供了一些很酷的环境配置。在新的models文件夹里,你可以用你的对象创建一个新文件,语法是...

module.exports = function(sequelize, DataTypes) {
  var User = sequelize.define("User", {
    username: {
      type: DataTypes.STRING
    }, 
    {
      freezeTableName: true // Model tableName will be the same as the model name 
    }
  });

  return User;
}


虽然我很喜欢这个工具,但这里的关键是要注意Sequelize将查找define()方法的第一个参数,所以我们可以写

module.exports = function(sequelize, DataType){
return sequelize.define("User", {
    username: {
      type: DataTypes.STRING
    }, 
    {
      freezeTableName: true // Model tableName will be the same as the model name 
    }
  });

aemubtdh

aemubtdh4#

在我的例子中,当我在迁移中使用无效类型时,我遇到了这个错误。假设这可能是问题所在,并从迁移中删除无效类型,它再次工作。

相关问题