问题描述
我收到 Unhandled reject TypeError: feed.create is not a function
错误,我不明白为什么会发生.这里有什么问题?
I'm getting Unhandled rejection TypeError: feed.create is not a function
error and I can't understand why it occurs. What's the problem here?
这是我的代码.我可能在这里没有做一些非常基本的事情,因为我无法在 routes/index.js 中访问 feed 变量.
Here's my code. I'm probably not doing something very fundamental here since I can't reach feed variable in routes/index.js.
如果我添加 module.exports = feed;到我的模型文件,我可以访问它,但我有多个模型,所以如果我在提要下方添加其他模型,它们会覆盖它.
If I add module.exports = feed; to my models file, I can reach it, but I have more than one models, so if I add additional models below the feed, they override it.
db.js
var Sequelize = require('sequelize');
var sequelize = new Sequelize('mydatabase', 'root', 'root', {
host: 'localhost',
dialect: 'mysql',
port: 8889,
pool: {
max: 5,
min: 0,
idle: 10000
},
define: {
timestamps: false
}
});
var db = {};
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
models.js
var db = require('./db'),
sequelize = db.sequelize,
Sequelize = db.Sequelize;
var feed = sequelize.define('feeds', {
subscriber_id: Sequelize.INTEGER,
activity_id: Sequelize.INTEGER
},
{
tableName: 'feeds',
freezeTableName: true
});
路由/index.js
routes/index.js
var express = require('express');
var router = express.Router();
var models = require('../models');
router.get('/addfeed', function(req,res) {
sequelize.sync().then(function () {
return feed.create({
subscriber_id: 5008,
activity_id : 116
});
}).then(function (jane) {
res.sendStatus(jane);
});
});
推荐答案
你不能从一个文件中访问一个变量,只需要在另一个文件中.您需要定义一个对象字面量来将所有变量保存在一个位置并将其分配给 module.exports
,或者您需要分别从不同的文件中导入它们.
You cannot reach a variable from a file, by only requiring it in another one. You need to either define an object literal to hold all your variables in one place and assign it to module.exports
, or you need to import them from different files separately.
在你的情况下,我会创建单独的文件来保存表模式,然后通过 sequelize.import
在一个文件下导入它们,然后需要该文件.
In your case, I would create separate files to hold table schemas, and then import them by sequelize.import
under one file, then require that file.
像这样:
模型/index.js:
models/index.js:
var sequelize = new Sequelize('DBNAME', 'root', 'root', {
host: "localhost",
dialect: 'sqlite',
pool:{
max: 5,
min: 0,
idle: 10000
},
storage: "SOME_DB_PATH"
});
// load models
var models = [
'Users',
];
models.forEach(function(model) {
module.exports[model] = sequelize.import(__dirname + '/' + model);
});
模型/Users.js
models/Users.js
var Sequelize = require("sequelize");
module.exports=function(sequelize, DataTypes){
return Users = sequelize.define("Users", {
id: {
type: DataTypes.INTEGER,
field: "id",
autoIncrement: !0,
primaryKey: !0
},
firstName: {
type: DataTypes.STRING,
field: "first_name"
},
lastName: {
type: DataTypes.STRING,
field: "last_name"
},
}, {
freezeTableName: true, // Model tableName will be the same as the model name
classMethods:{
}
},
instanceMethods:{
}
}
});
};
然后像这样导入每个模型:
Then import each model like this:
var Users = require("MODELS_FOLDER_PATH").Users;
希望这会有所帮助.
这篇关于sequelize .create 不是函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!