Sequelize .save - undefined不是构造函数

时间:2016-12-03 12:08:35

标签: postgresql promise ecmascript-6 sequelize.js

我正在尝试使用Sequelize将一些数据保存到Postgres数据库。这是我连接数据库并创建模型的代码:

import Sequelize from 'sequelize'

const getModels = Connection => {
  return {
    User: Connection.define('users', {
      id: { type: Sequelize.STRING, primaryKey: true },
      refresh_token: { type: Sequelize.STRING }
    })
  }
}

const getInstance = () => {
  const Connection = new Sequelize(
    process.env.DATABASE_URL, {
      dialect: 'postgres',
      logging: process.env.NODE_ENV === 'production' ? false : console.log,
      omitNull: true,
    }
  )
  return getModels(Connection)
}

export default getInstance

我尝试在用户登录时保存用户idrefresh_token(使用Auth0进行身份验证)。这是我的代码:

const {User} = getInstance()
const user = User.build({ id: user_id, refresh_token: refresh_token })
user.save().then(() => {
  console.log('save completed')
}).catch(error => {
  console.log(`Something went wrong: ${error}`)
})

我收到此错误:Something went wrong: TypeError: undefined is not a constructor (evaluating 'new net.Stream()')

如果我console.log(user.id)user.refresh_token,我会收到正确的信息,以便.build功能正常运行。但是保存功能有问题。感谢您解决它的任何帮助 - 我正在使用带有Webpack的React应用程序(以及服务器端呈现);由于Sequelize我在前端而不是后端使用它,因此我从Webpack获得了一些关于依赖关系的警告。如果有人对如何解决这些问题有任何想法,那就太棒了:

[0] ./~/sequelize/lib/sequelize.js
[0] Critical dependencies:
[0] 686:60-73 the request of a dependency is an expression
[0]  @ ./~/sequelize/lib/sequelize.js 686:60-73
[0] ./~/sequelize/lib/dialects/mssql/connection-manager.js
[0] Critical dependencies:
[0] 18:15-71 the request of a dependency is an expression
[0]  @ ./~/sequelize/lib/dialects/mssql/connection-manager.js 18:15-71
[0] ./~/sequelize/lib/dialects/mysql/connection-manager.js
[0] Critical dependencies:
[0] 18:17-60 the request of a dependency is an expression
[0]  @ ./~/sequelize/lib/dialects/mysql/connection-manager.js 18:17-60
[0] ./~/sequelize/lib/dialects/postgres/connection-manager.js
[0] Critical dependencies:
[0] 20:14-57 the request of a dependency is an expression
[0]  @ ./~/sequelize/lib/dialects/postgres/connection-manager.js 20:14-57
[0] ./~/sequelize/lib/dialects/sqlite/connection-manager.js
[0] Critical dependencies:
[0] 22:15-71 the request of a dependency is an expression
[0]  @ ./~/sequelize/lib/dialects/sqlite/connection-manager.js 22:15-71
[0] webpack built 3215b07a25217c8e67e0 in 13519ms

谢谢!

0 个答案:

没有答案