使用node和sequelize

时间:2019-05-11 04:51:52

标签: javascript node.js postgresql typescript sequelize.js

我正在使用节点序列库在postgress db中插入数据

这是我在Users.ts文件中的用户模型

export class User extends Sequelize.Model {
  public id!: number;
  public name: string;
  public email: string;
  public googleId: string;

  public password: string;
  // public preferredName!: string | null; // for nullable fields

  // timestamps!
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;

  async comparePassword(candidatePassword: string, callback: Function) {
    try {
      const isMatch = await bcrypt.compare(candidatePassword, this.password);
      callback(undefined, isMatch);
    } catch (err) {
      callback(err);
    }
  }
}

User.init(
  {
    id: {
      type: Sequelize.DataTypes.INTEGER.UNSIGNED,
      autoIncrement: true,
      primaryKey: true,
    },
    name: {
      type: Sequelize.DataTypes.STRING,
      allowNull: false,
    },
    email: {
      type: Sequelize.DataTypes.STRING(128),
      allowNull: false,
      unique: true,
    },
    googleId: {
      type: Sequelize.DataTypes.STRING(128),
      allowNull: true,
    },
    password: {
      type: Sequelize.DataTypes.STRING(300),
      allowNull: true,
    },
    token: {
      type: Sequelize.DataTypes.STRING(300),
      allowNull: true,
    },
  },
  {

    tableName: 'users',
    sequelize: sequelize, // this bit is important
  }
);

当我尝试使用db在数据库中创建新记录时

await User.create({name, email, password, address});

无论输入什么,它总是会返回无效值的错误

  

错误:Object.escape处的值无效{名称:'d'}   (/用户/ eali /学习/ akasa服务器/node_modules/sequelize/lib/sql-string.js:65:11)   在PostgresQueryGenerator.escape   (/用户/ eali /学习/ akasa服务器/node_modules/sequelize/lib/dialects/abstract/query-generator.js:964:22)   在PostgresQueryGenerator._whereParseSingleValueObject中   (/用户/ eali /学习/ akasa服务器/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2431:41)   在PostgresQueryGenerator.whereItemQuery   (/用户/ eali /学习/ akasa服务器/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2149:19)   在Utils.getComplexKeys.map.prop   (/用户/ eali /学习/ akasa服务器/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2206:21)   在Array.map()在PostgresQueryGenerator._whereBind   (/用户/ eali /学习/ akasa服务器/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2204:43)   在PostgresQueryGenerator.whereItemQuery   (/用户/ eali /学习/ akasa服务器/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2139:19)   在Utils.getComplexKeys.forEach.prop   (/用户/ eali /学习/ akasa服务器/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2048:25)   在Array.forEach()在   PostgresQueryGenerator.whereItemsQuery   (/用户/ eali /学习/ akasa服务器/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2046:35)   在PostgresQueryGenerator.getWhereConditions   (/用户/ eali /学习/ akasa服务器/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2458:19)   在PostgresQueryGenerator.selectQuery中   (/用户/ eali /学习/ akasa服务器/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1279:28)   在QueryInterface.select   (/用户/ eali /学习/ akasa服务器/node_modules/sequelize/lib/query-interface.js:1120:27)   然后在Promise.try.then.then   (/用户/ eali /学习/ akasa服务器/node_modules/sequelize/lib/model.js:1748:34)   在tryCatcher   (/用户/ eali /学习/ akasa服务器/node_modules/bluebird/js/release/util.js:16:23)   在Promise._settlePromiseFromHandler   (/用户/ eali /学习/ akasa服务器/node_modules/bluebird/js/release/promise.js:512:31)   在Promise._settlePromise   (/用户/ eali /学习/ akasa服务器/node_modules/bluebird/js/release/promise.js:569:18)   在Promise._settlePromise0   (/用户/ eali /学习/ akasa服务器/node_modules/bluebird/js/release/promise.js:614:10)   在Promise._settlePromises   (/用户/ eali /学习/ akasa服务器/node_modules/bluebird/js/release/promise.js:694:18)   在_drainQueueStep   (/用户/ eali /学习/ akasa服务器/node_modules/bluebird/js/release/async.js:138:12)   在_drainQueue   (/Users/eali/learn/akasa-server/node_modules/bluebird/js/release/async.js:131:9)

1 个答案:

答案 0 :(得分:0)

我的第一个猜测是您忘记为属性键添加值:

await User.create({name, email, password, address});

应为:

await User.create({name: 'john wayne', email: 'lol@example.com', password: '1234', address: 'Someplace 1'});