2016-11-19 6 views
0

私が使用しようとnodejsにcaminteJsを実装するよ、私はmodelsフォルダを作成し、その中で私はindex.jsNodejsは

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

として分離したファイルの中に包まれた、いくつかのデータベーススキーマを持っているのを

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

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

user.jsファイルの内容::

0123私が持っている modelsフォルダに

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

server.listen(port, function() { 
    console.log('Server listening at port %d', port); 
}); 

io.on('connection', function (socket) { 
    socket.on("new_channel", function (data, device) { 
     new models.channels({ channel_name: 'Peter' }); 
     console.log(channel); 
    }); 
}); 

私はuser.jsに、このエラーを取得する:

TypeError: schema.define is not a function 

答えて

0

おそらくそれは次のようになります。

module.exports = { 
    caminte: caminte, 
    Schema: Schema, 
    config: config, 
    schema: schema // <- this 
} 

だからuser.jsschemaはあなたがmodule.exportsで定義された同じオブジェクトです:

var users = schema.schema.define('channels', { 
あなたがここにいることを定義したため。

あなたはこのようuser.jsを書き換えることができます:

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

    return users; 
}; 
+0

おかげで、問題が解決します –