【问题标题】:Sequelize .create is not generating content for my modelSequelize .create 没有为我的模型生成内容
【发布时间】:2018-08-06 04:30:09
【问题描述】:

我到处寻找这个问题,但没有找到。 我正在为 Postgres 使用 Sequelize,并且正在尝试将内容添加到模型中。它没有抛出任何错误,只是一个警告说基于字符串的操作已被弃用,但这不是一个操作。

sequelize deprecated 基于字符串的运算符现在已被弃用。请使用基于符号的运算符以获得更好的安全性,在http://docs.sequelizejs.com/manual/tutorial/querying.html#operators 阅读更多内容

这是我的模型和 .create 代码

const Sequelize = require('sequelize');
const sequelizeInstance = new Sequelize('journal_entries', 'KupidoExportLLC', 
{
  dialect: 'postgres'
});



module.exports = function(sequelizeInstance, DataTypes){
const Entries = sequelizeInstance.define('entries', {
  user: Sequelize.STRING,
  title: Sequelize.STRING,
  content: Sequelize.TEXT
})

Entries.sync().then(function(){
  Entries.create({
    user: 'I need to get this to work',
    title: 'Ill be really happy when this does work',
    content: 'It will work this time'
  })
});



return Entries;

}

这是我的命令行显示的内容

sequelize deprecated 基于字符串的运算符现在已被弃用。请使用基于符号的运算符以获得更好的安全性,在http://docs.sequelizejs.com/manual/tutorial/querying.html#operators node_modules/sequelize/lib/sequelize.js:242:13 阅读更多信息

当我去看看我的桌子时,它是空的

journal_entries=# SELECT * FROM entries;
 id | user | title | content | createdAt | updatedAt
----+------+-------+---------+-----------+-----------
(0 rows)

我不知道还能是什么

【问题讨论】:

  • 下面的答案有帮助吗?据我所知应该可以接受

标签: node.js postgresql sequelize.js models


【解决方案1】:

你在这里使用 Promise,你必须等待或返回它们。 如果您的节点版本中有异步等待可用,您可以这样做:

module.exports = async function(sequelizeInstance, DataTypes){

  const Entries = sequelizeInstance.define('entries', {
    user: Sequelize.STRING,
    title: Sequelize.STRING,
    content: Sequelize.TEXT
  });

  await sequelizeInstance.sync();
  await Entries.create({
    user: 'I need to get this to work',
    title: 'Ill be really happy when this does work',
    content: 'It will work this time'
  });

  const entries = await Entries.findAll();
  console.log(entries.map(e => e.get({ plain: true })));
  return Entries;
};

如果不是

module.exports = async function(sequelizeInstance, DataTypes){

  const Entries = sequelizeInstance.define('entries', {
    user: Sequelize.STRING,
    title: Sequelize.STRING,
    content: Sequelize.TEXT
  });

  return sequelizeInstance.sync().then(() => {

    return Entries.create({
      user: 'I need to get this to work',
      title: 'Ill be really happy when this does work',
      content: 'It will work this time'
    });
  })
  .then(() => {
    return Entries.findAll().then(entries => {
      console.log(entries.map(e => e.get({ plain: true })));
      return entries;
    })
  });
};

【讨论】:

  • 这绝对有帮助,非常感谢!事实证明,问题根本不是已弃用的字符串消息
【解决方案2】:

您需要关闭警告。

在您的 config.JSON 文件中或(如果您已在同一文件中初始化连接) 按照给定的方式关闭警告:

IN Config.JSON

{
  "development": {
    "username": "root",
    "password": "12345",
    "database": "fasttrack",
    "host": "localhost",
    "dialect": "mysql",
    "define": {
      "timestamps": false
  },
  "operatorsAliases": false
  },
  "test": {
    "username": "root",
    "password": "12345",
    "database": "fasttrack",
    "host": "localhost",
    "dialect": "mysql",
    "define": {
      "timestamps": false
  },
  "operatorsAliases": false
  },
  "production": {
    "username": "root",
    "password": "12345",
    "database": "fasttrack",
    "host": "localhost",
    "dialect": "mysql",
    "define": {
      "timestamps": false
  },
  "operatorsAliases": false
  }
}

内联连接声明

const sequelize = new Sequelize(config.database, config.username, config.password, { 主机:config.host, 方言:config.dialect, 水池: { 最大:5, 分钟:0, 闲置:10000 }, 记录:假, 冻结表名:真, operatorsAliases:假 });

【讨论】:

    猜你喜欢
    • 2020-05-29
    • 2020-01-04
    • 2018-04-14
    • 2016-04-17
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    相关资源
    最近更新 更多