续串不是函数错误,为什么?

时间:2019-01-14 11:20:47

标签: javascript node.js sequelize.js

请帮助。 为什么我在昵称对象中收到String(150)的奇怪错误?

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不是函数         在对象。 (E:\ Projects \ JavaScript \ couponsystem \ src \ models \ schema.js:21:22)         在Module._compile(内部/模块/cjs/loader.js:688:30)         在Object.Module._extensions..js(内部/模块/cjs/loader.js:699:10)         在Module.load(internal / modules / cjs / loader.js:598:32)         在tryModuleLoad(内部/模块/cjs/loader.js:537:12)         在Function.Module._load(internal / modules / cjs / loader.js:529:3)         在Module.require(内部/模块/cjs/loader.js:636:17)         在要求时(内部/模块/cjs/helpers.js:20:18)         在对象。 (E:\ Projects \ JavaScript \ couponsystem \ src \ models \ generator.js:1:78)         在Module._compile(内部/模块/cjs/loader.js:688:30)         在Object.Module._extensions..js(内部/模块/cjs/loader.js:699:10)         在Module.load(internal / modules / cjs / loader.js:598:32)         在tryModuleLoad(内部/模块/cjs/loader.js:537:12)         在Function.Module._load(internal / modules / cjs / loader.js:529:3)         在Function.Module.runMain(内部/模块/cjs/loader.js:741:12)         在启动时(internal / bootstrap / node.js:285:19)         在bootstrapNodeJSCore(internal / bootstrap / node.js:739:3)

  

2 个答案:

答案 0 :(得分:3)

由于无法使用sequelize访问数据类型,我开始对此模块的主类进行审查。

执行此操作const Sequelize = require('sequelize');时,您正在调用扩展数据类型的接口(它们也是接口),因此,您可以毫无问题地使用BIGINT或STRING。

但是当您这样做

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

您指的是上述接口的构造函数(构造签名)。这导致sequelize中的对象是另一个类或函数的实例,而不是对Sequelize的直接引用,这就是它们具有不同属性的原因。


总之,这将是执行您请求的代码实现的正确方法。

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
    }
}, {});
  

接口中的构造签名不能在类中实现;它们仅用于定义现有的JS API,这些API定义了“新”功能   功能。

答案 1 :(得分:1)

这应该可以工作,有关更多信息,请阅读下面的文档。

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