Tag: sequelize.js

使用Sequelize和Express进行API路由时出错

我有一个问题,追查为什么我的路由失败。 我是Sequelize和Express路由的新手。 目标 – 访问API端点'/ v1 / agent /:id'时,我想从Sequelize查询返回一个JSON响应。 我已经确认查询起作用,并将一行映射到我的Agent模型。 当我启动应用程序,我得到一个Router.use() requires middleware function but got a ' + gettype(fn)); 来自Node的exception。 来自initializeDb函数的exception,但我不知道为什么。 这里是根index.js: import http from 'http'; import bodyParser from 'body-parser'; import express from 'express'; import sequelize from 'sequelize'; import config from './config'; import routes from './routes'; let app = express(); app.server = http.createServer(app); app.use(bodyParser.json({ […]

使用node.js实现触发器sequelize和hooks

我正在尝试创build一个钩子,应该在为logging创build主键后调用。 我正在尝试使用插入的列的组合(包括主键)来生成一个数字。 实例钩子似乎被调用,而全局钩子没有被调用,hewever字段保持回来,即使我看到挂钩叫: module.exports = function(sequelize, DataTypes) { var store = sequelize.define('store', { storenumber: DataTypes.STRING(30), //remove storespecificationid: DataTypes.INTEGER, storetypeid: DataTypes.INTEGER, storename: DataTypes.STRING(20), //remove address: DataTypes.STRING(30), city: DataTypes.STRING(30), state: DataTypes.STRING(30), zipcode: DataTypes.STRING(30) }, { classMethods: { associate: function(models) { // associations can be defined here }, hooks: { /*this is not getting called*/ afterCreate: function(store){ let […]

Sequelize包含嵌套对象数组 – node.js

我有关系: (Track) – M:1 – >(TrackChannel)< – 1:M(Channel) (User) – M:1 – >(UserChannel)< – 1:M(Channel) Channel模型包含对象Track作为current_track_id与关系一对一。 Track和Channel通过TrackChannel相关TrackChannel User和Channel通过UserChannel相关UserChannel / * Channel.js:* / module.exports = function(sequelize, DataTypes) { return sequelize.define('channel', { id: { allowNull: false, primaryKey: true, type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, }, current_counter: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0 }, track_counter: { type: DataTypes.INTEGER, allowNull: […]

续集迁移脚本抛出errorUnhandled拒绝SequelizeBaseError:SQLITE_ERROR:没有这样的表

我已经写了sequelizer的迁移javascript来改变现有的列在一个表中,但是当我运行命令sequelizer db:migrate ,它给出的错误 == 20170212050240-alter_col_tag_subject: migrating ======= Unhandled rejection SequelizeBaseError: SQLITE_ERROR: no such table: subject_tags == 20170212050240-alter_col_tag_subject: migrated (0.241s) 这里是迁移文件的代码 'use strict'; module.exports = { up: function (queryInterface, Sequelize) { return [ queryInterface.changeColumn( 'subject_tags', 'tag', { type: Sequelize.STRING, unique: true, validate: { len: [1, 250] } } ), queryInterface.changeColumn( 'subject_tags', 'tag_description', { type: Sequelize.STRING, allowNull: […]

mysql在nodejs中search子节点和父节点表字段

在我的Node应用程序中,我们使用Sequelize来连接mysql有两个表User & UserOption User表有以下字段 user_id(pk) user_email UserOption表有以下字段 option_id(pk) user_id(fk) first_name 我需要通过search文本在user_email & first_name列出所有用户是否有任何选项在user_email中search父&子表格字段? UPDATE User表 user_id user_email 1 text@text.com 2 anything@anything.com 3 jhon@smthng.com UserOption表 option_id user_id first_name 1 1 jhon 2 2 smith 3 3 david 如果我search“jhon”,结果将是id为1和2的用户

如何以编程方式自动化sequelize请求语句?

我使用sequelize作为Object Relational Mapper连接到PostgreSQL数据库。 下面的语句很好,但我不得不手动编写每个请求。 global.db.dataBaseTable.build().instanceMethod(successcb, data, errcb); 有没有办法使用充满命令的数组来构build这个语句来使用循环创build多个语句? 以下是我使用的代码的一个例子,但是编译器会返回错误。 var ary_db_table = ["aTable", "bTable", "cTable"] for(var i = 0; i<=1; i++){ global.db.ary_db_table[i].build().instanceMethod(successcb, data, errcb) }

如何在types脚本中编写sequelize事务

即时通讯使用sequelize,节点js和types脚本。 我需要转换下面的命令来键入脚本。 return sequelize.transaction().then(function (t) { return User.create({ firstName: 'Homer', lastName: 'Simpson' }, {transaction: t}).then(function (user) { return user.addSibling({ firstName: 'Lisa', lastName: 'Simpson' }, {transaction: t}); }).then(function () { return t.commit(); }).catch(function (err) { return t.rollback(); }); }); 任何人都可以帮我解决这个问题,或者给出一些关于types脚本中的sequizeize事务的例子 谢谢你的进步

将关联添加到sequelizejs模型

我已经定义了一个模型(通过使用sequelize-cli): 'use strict'; module.exports = function(sequelize, DataTypes) { var Rating = sequelize.define('Rating', { star_count: DataTypes.FLOAT, details: DataTypes.TEXT }, { classMethods: { associate: function(models) { Rating.hasOne(models.Product); } } }); return Rating; }; 但是,当我查询数据库中的数据库后I db:migrate我得到: SELECT * from "Ratings"; id | star_count | details | createdAt | updatedAt —-+————+———+———–+———– 文件真的没有什么比我上面做的不同。 我希望在这里看到一个product_id字段。 我究竟做错了什么? 编辑: Sequelize-CLI在模型中创build一个名为index.js的文件。 它看起来像这样: 'use strict'; […]

声明关系的续集?

我有三个模型,我试图定义它们之间的关系。 模型:用户付款债务人 用户有很多付款 付款属于用户 付款有许多债务人 债务人属于付款 债务人属于付款 我需要在源和目标中定义关系吗? 即 User.hasMany(Payment, {as: 'Payments'}) 并且 Payment.belongsTo(User) 或者我可以摆脱只是在一个模型中定义它? 试图在两者中定义它时,我似乎都会遇到错误。 我导入的模型是undefined ,因此当我尝试将它传递给“belongsTo()”或“hasMany()”函数时,我得到的错误called with something that's not an instance of Sequelize.Model 当我只将它包含在一个模型中时,它不会正确地创build关系。 我的模特如下: 付款方式: import { sequelize } from '../config'; import Sequelize from 'sequelize'; import User from './User'; const Payment = sequelize.define('payment', { amount: { type: Sequelize.FLOAT }, reference: { […]

Node.js代码只能在文件顶部使用sequelize

当在我的服务器端代码的顶部,这工作正常,产生的结果是正确的: var data_playlists = {}; models.Playlist.findAll({ attributes: ['id', 'name'] }).then(function (playlists){ data_playlists['playlists'] = playlists.map(function(playlist){ return playlist.get({plain: true}) }); addsongs(data_playlists, 1); addsongs(data_playlists, 2); addsongs(data_playlists, 3); }); 但是当它在我的一个Express方法中时,它运行不正常。 特别是,扩展方法不能正常工作。 function addsongs(playlist_object, id_entered){ var arraysongs = []; models.Playlist.findOne({ attributes: ['id'], where: { id: id_entered } }) .then(function(playlist) { playlist.getSongs().then(function (thesongs){ for(var k = 0; k < thesongs.length ; […]