javascript - Node 和 Sequelize 新手,并获得 "Unhandled rejection TypeError: Vacation.create is not a function"

标签 javascript node.js express sequelize.js

我正在尝试在我的node.js 项目中使用sequelize 播种数据。

我收到的错误是:

未处理的拒绝类型错误: 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)
})
});

我的模型 Vacation.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 属性的对象。

您应该直接导出假期:

假期.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/

相关文章:

node.js - nodejs中用于测试和开发的不同环境变量

Node.js maxFieldSize 超出

javascript - 单个<select>有多个选中选项

javascript - 需要 riak-js 的帮助

javascript - 修改 WordPress.com 的 CSS 自托管 Wordpress 网站的订阅按钮

node.js - 在express中将 "hook"放入app.response函数的正确方法是什么?

node.js - Stripe webhook 测试错误 302

javascript - 根据位置/日期更改日期选择器中的时间

node.js - HTTPS 与 NodeJS 设置请求 header

javascript - 为什么我能够将 require 的结果分配给一个对象?