Javascript Nodejs模型不是构造函数
我试图在nodejs应用程序上实现Javascript Nodejs模型不是构造函数,javascript,node.js,Javascript,Node.js,我试图在nodejs应用程序上实现caminetjs,在单独的文件上定义数据库模式后,我无法使用它,我得到以下错误: TypeError: models.channels.create is not a constructor 我创建了models文件夹,其中包含一些数据库模式,这些模式被包装到单独的文件中,如 user.js //contains user table schema news.js //contains news table schema 进入index.js,它位于我的m
caminetjs
,在单独的文件上定义数据库模式后,我无法使用它,我得到以下错误:
TypeError: models.channels.create is not a constructor
我创建了models文件夹,其中包含一些数据库模式,这些模式被包装到单独的文件中,如
user.js //contains user table schema
news.js //contains news table schema
进入index.js
,它位于我的models文件夹中:
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;
};
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
}
config.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;
};
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
}
然后使用myserver.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 );
} );
} );
根据您想要的内容和架构的外观,但在创建时请尝试使用:
const user = models.users.create(function(err){
console.log('Error happened', err);
});
@jfriend00
models
是一个文件夹,其中有一个index.js
和一些文件作为数据库模式,我使用这些文件来导入所有文件,请参阅我的文章的这一部分,作为导入index.js,它位于models文件夹中:
您可以使用model.entity.create(errCallback)
或new model.entity()
和entity.save(callback)
但不是new
的第一个选项。请参见编辑。