我正在尝试在我的 node.js 项目中使用 sequelize 播种数据。
我得到的错误是:
未处理的拒绝 TypeError:Vacation.create 不是函数
我的 Node js 应用程序包含:
Vacation = require('./models/vacation.js');
var app = express();
var Sequelize = require('sequelize');
sequelize.sync().then(function () {
Vacation.create({
// insert new user
name: 'Hood River Day Trip',
slug: 'hood-river-day-trip',
category: 'Day Trip',
sku: 'HR199',
description: 'Spend a day sailing on the Columbia and ' +
'enjoying craft beers in Hood River!',
priceInCents: 9995,
tags: ['day trip', 'hood river', 'sailing', 'windsurfing', 'breweries'],
inSeason: true,
maximumGuests: 16,
available: true,
packagesSold: 0
},
{
name: 'Oregon Coast Getaway',
slug: 'oregon-coast-getaway',
category: 'Weekend Getaway',
sku: 'OC39',
description: 'Enjoy the ocean air and quaint coastal towns!',
priceInCents: 269995,
tags: ['weekend getaway', 'oregon coast', 'beachcombing'],
inSeason: false,
maximumGuests: 8,
available: true,
packagesSold: 0,
},
{
name: 'Rock Climbing in Bend',
slug: 'rock-climbing-in-bend',
category: 'Adventure',
sku: 'B99',
description: 'Experience the thrill of rock climbing in the high desert.',
priceInCents: 289995,
tags: ['weekend getaway', 'bend', 'high desert', 'rock climbing', 'hiking', 'skiing'],
inSeason: true,
requiresWaiver: true,
maximumGuests: 4,
available: false,
packagesSold: 0,
notes: 'The tour guide is currently recovering from a skiing accident.'
}).success(function (data) {
console.log(data.values)
})
});
和我的模型假期.js包含
var Sequelize = require('sequelize');
module.exports = function (sequalize) {
var Vacation = sequelize.define("Vacation", {
name: Sequelize.STRING,
slug: Sequelize.STRING,
category: Sequelize.STRING,
sku: Sequelize.STRING,
description: Sequelize.STRING,
priceInCents: Sequelize.INTEGER,
tags: [Sequelize.STRING],
inSeason: Sequelize.BOOLEAN,
available: Sequelize.BOOLEAN,
requiresWaiver: Sequelize.BOOLEAN,
maximumGuests: Sequelize.INTEGER,
notes: Sequelize.STRING,
packagesSold: Sequelize.INTEGER,
});
return {
Vacation: Vacation
};
};
最佳答案
vacation.js
使用 Vacation
属性导出和对象。这意味着导入:
Vacation = require('./models/vacation.js');
接收具有
Vacation
属性但没有 create
属性的对象。您应该直接导出
Vacation
:假期.js
module.exports = function(sequelize) {
var Vacation = sequelize.define('Vacation', {
// Model field definitions
});
return Vacation;
};
关于javascript - Node 和 Sequelize 的新手并获得 "Unhandled rejection TypeError: Vacation.create is not a function",我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35191110/