0
votes

Please need your help . Why i got the strange error for String(150) in nickname object ?

const Sequelize = require('sequelize');
const devOptions = require('./../config/config').connectionConf.development;

const sequelize = new Sequelize(
  devOptions.database,
  devOptions.username,
  devOptions.password,
  {
    host: devOptions.host,
    dialect: devOptions.dialect
  }
);

const User = sequelize.define('user', {
    user_id: {
      primaryKey:true,
      type:sequelize.BIGINT,
      allowNull:false 
   },
    nickname: {
      type:sequelize.STRING(150),
      unique:true,
      allowNull:false
    },
    email: {
      type:sequelize.STRING(150),
      unique:true,
      allowNull:false
    },
    user_password: {
      type:sequelize.STRING(150),
      unique:true,
      allowNull:false
    },
    created_at: {
      type:sequelize.Date,
      created_time:sequelize.NOW,
      allowNull:false
    },
    updated_at: {
      type:sequelize.Date,
      allowNull:false
    }
  }, {});

TypeError: sequelize.STRING is not a function at Object. (E:\Projects\JavaScript\couponsystem\src\models\schema.js:21:22) at Module._compile (internal/modules/cjs/loader.js:688:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10) at Module.load (internal/modules/cjs/loader.js:598:32) at tryModuleLoad (internal/modules/cjs/loader.js:537:12) at Function.Module._load (internal/modules/cjs/loader.js:529:3) at Module.require (internal/modules/cjs/loader.js:636:17) at require (internal/modules/cjs/helpers.js:20:18) at Object. (E:\Projects\JavaScript\couponsystem\src\models\generator.js:1:78) at Module._compile (internal/modules/cjs/loader.js:688:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10) at Module.load (internal/modules/cjs/loader.js:598:32) at tryModuleLoad (internal/modules/cjs/loader.js:537:12) at Function.Module._load (internal/modules/cjs/loader.js:529:3) at Function.Module.runMain (internal/modules/cjs/loader.js:741:12) at startup (internal/bootstrap/node.js:285:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)

2

2 Answers

3
votes

Intrigued by the fact that you couldn't access the data types with sequelize, I started to review the main class of the module.

When you do this const Sequelize = require('sequelize'); you are calling an interface that extends the data types (they are also an interface), for that reason you can use BIGINT or STRING without any problem.

But when you do this

const sequelize = new Sequelize(
    devOptions.database,
    devOptions.username,
    devOptions.password,
    {
        host: devOptions.host,
        dialect: devOptions.dialect
    }
);

You are referring to a constructor (Construct signatures) of the interface mentioned above. This causes that the object that is in sequelize is the instance to another class or function and not a direct reference to Sequelize, that is why they have different properties.


In conclusion this would be the correct way to do the code implementation you request.

const Sequelize = require('sequelize');
const devOptions = require('./../config/config').connectionConf.development;

const sequelize = new Sequelize(
    devOptions.database,
    devOptions.username,
    devOptions.password,
    {
        host: devOptions.host,
        dialect: devOptions.dialect
    }
);

const User = sequelize.define('user', {
    user_id: {
        primaryKey: true,
        type: Sequelize.BIGINT,
        allowNull: false
    },
    nickname: {
        type: Sequelize.STRING(150),
        unique: true,
        allowNull: false
    },
    email: {
        type: Sequelize.STRING(150),
        unique: true,
        allowNull: false
    },
    user_password: {
        type: Sequelize.STRING(150),
        unique: true,
        allowNull: false
    },
    created_at: {
        type: Sequelize.Date,
        created_time: Sequelize.NOW,
        allowNull: false
    },
    updated_at: {
        type: Sequelize.Date,
        allowNull: false
    }
}, {});

Construct signatures in interfaces are not implementable in classes; they're only for defining existing JS APIs that define a 'new'-able function.

1
votes

This should work, for more info, read the docs below.

const Sequelize = require('sequelize');
const devOptions = require('./../config/config').connectionConf.development;

const sequelize = new Sequelize(
  devOptions.database,
  devOptions.username,
  devOptions.password,
  {
    host: devOptions.host,
    dialect: devOptions.dialect
  }
);

const User = sequelize.define('user', {
    user_id: {
      primaryKey:true,
      type:sequelize.BIGINT,
      allowNull:false 
   },
    nickname: {
      type:sequelize.STRING,
      unique:true,
      allowNull:false
    },
    email: {
      type:sequelize.STRING,
      unique:true,
      allowNull:false
    },
    user_password: {
      type:sequelize.STRING,
      unique:true,
      allowNull:false
    },
    created_at: {
      type:sequelize.Date,
      created_time:sequelize.NOW,
      allowNull:false
    },
    updated_at: {
      type:sequelize.Date,
      allowNull:false
    }
  }, {});

Docs, Extra info