Nodejs模型不是构造函数

时间:2016-11-19 09:25:04

标签: javascript node.js

我试图在nodejs应用程序上实现caminteJs,在分离文件上定义数据库架构之后我无法使用它并且我收到此错误:

TypeError: models.channels.create is not a constructor

我创建了模型文件夹,并且我有一些数据库模式,它包含在单独的文件中

user.js //contains user table schema
news.js //contains news table schema

进入index.js进入模型文件夹我有:

var users = require( './user' );
var news = require( './news' );

module.exports = {
    users:users,
    news:news
};

user.js文件内容:

var config = require('../config');
module.exports = function(schema){
    var users = config.define('users', {
         user_name: { type: schema.String, limit: 30 },
         ...
         created_at: { type: schema.Date },
         updated_at: { type: schema.Date }
    });

    return users;
};

config.js文件内容:

var caminte = require( 'caminte' ),
    Schema = caminte.Schema,
    config = {
        driver: "mysql",
        host: "localhost",
        port: "3306",
        username: "root",
        password: "",
        database: "test",
        pool: true
    },
    schema = new Schema( config.driver, config );

module.exports = {
    caminte: caminte,
    Schema: Schema,
    config: config,
    schema: schema
}

然后我的server.js使用它们:

var socket = require( 'socket.io' ),
    ...
    config = require( './config' ),
    models = require( './models' );

io.on( 'connection', function (socket) {
    socket.on( "new_user", function (data, device) {
        const channels = new models.users.create(function(err){
            user_name: 'Peter'
        });

        console.log( channels );
    } );
} );

1 个答案:

答案 0 :(得分:0)

根据您的需要以及架构的外观,但创建时请尝试使用:

 const user = models.users.create(function(err){
            console.log('Error happened', err);
        });