gpt4 book ai didi

node.js - 通过将模型逻辑与模型配置分离来对问题进行后续处理

转载 作者:行者123 更新时间:2023-12-03 22:28:52 25 4
gpt4 key购买 nike

我遵循 sequelize 最佳实践,方法是连接到主机并将所有模型导入一个文件,然后在与模型交互时调用该文件。出于某种原因,这似乎导致了问题,因为我在将 define 方法用于 sequelize 变量时出错,并且我使用包含两个逻辑的文件进行了测试,并且能够添加用户。

错误:

TypeError: Cannot read property 'define' of undefined
at new module.exports (/Users/user/Desktop/Projects/node/ann/app/models/ann-model.js:3:27)
at /Users/user/Desktop/Projects/node/ann/app/controllers/appRoutes.js:13:20

这是我的文件,其中包含与数据库的连接 (dbIndex.js):
var Sequelize = require('sequelize');
var sequelize = new Sequelize('dbname', 'user', 'pwd', {
host:'localhost',
port:'3306',
dialect: 'mysql'
});

sequelize
.authenticate()
.then(function(err) {
if (!!err) {
console.log('Unable to connect to the database:', err)
} else {
console.log('Connection has been established successfully.')
}
});

var db = {}

db.Ann = sequelize.import(__dirname + "/ann-model");

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

这是模型文件 (ann-model.js):
module.exports = function(sequelize, DataTypes) {

var Ann = sequelize.define('ann', {
ann_id: {
type: DataTypes.INTEGER,
primaryKey: true
},
ann_date: DataTypes.DATE,
}, {
freezeTableName: true
});
return Ann;
}

这是我使用 POST 方法调用模型的地方。

(appRoutes.js):
var express = require('express');
var appRoutes = express.Router();
var Annotation = require('../models/ann-model');

appRoutes.route('/')

.get(function(req, res){
res.render('pages/activity-feed.hbs');
})

.post(function(req, res){

var ann = new Ann();

ann.ann_date = req.body.ann-date;


annotation.save(function(err){
if (err)
res.send(err);
});
});

module.exports = appRoutes;

将逻辑和建模结合在一个文件中的测试文件:
var Sequelize = require('sequelize');
var sequelize = new Sequelize('dbname', 'user', 'pwd', {
host:'localhost',
port:'3306',
dialect: 'mysql'
});


var Ann = sequelize.define('ann', {
ann_id: {
type: Sequelize.INTEGER,
primaryKey: true
},
ann_date: Sequelize.DATE,
}, {
freezeTableName: true
});

sequelize.sync().then(function(){
return Ann.create({
ann_id: 3,
discovery: 'This is a test.'
});
}).then(function(tation) {
console.log(tation.get({
plain: true
}));
});

最佳答案

我认为你的 post 方法有问题,但结构真的很困惑,所以你能这样做吗

模型/index.js

"use strict";

var fs = require("fs");
var path = require("path");
var Sequelize = require("sequelize");
var env = process.env.NODE_ENV || "development";
var config = require(__dirname + '/../config/config.json')[env];
var sequelize = new Sequelize(config.database, config.username, config.password, config);
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;
db.Sequelize = Sequelize;

module.exports = db;

模型/ann-model.js
"use strict";

module.exports = function(sequelize, DataTypes) {

var Ann = sequelize.define('ann', {
ann_id: {
type: DataTypes.INTEGER,
primaryKey: true
},
ann_date: DataTypes.DATE,
}, {
freezeTableName: true
});
return Ann;
}

路由/index.js
var express = require('express');
var appRoutes = express.Router();
var models = require('../models');

appRoutes.route('/')

.get(function(req, res){
res.send('ok');
})

.post(function(req, res){

models.ann
.build({ ann_id: 55, ann_date: new Date() })
.save()
.then(function(anotherTask) {
res.send("POST OK");
}).catch(function(error) {
res.send(error);
})

});

module.exports = appRoutes;

关于node.js - 通过将模型逻辑与模型配置分离来对问题进行后续处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33977761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com