我到处寻找这个问题,但我还没找到。我正在使用Sequelize for Postgres,我正在尝试将内容添加到模型中。它不会抛出任何错误,只是警告说基于字符串的操作已被弃用,但这不是一个操作。
sequelize deprecated基于String的运算符现已弃用。请使用基于符号的运算符以获得更好的安全性,请在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基于String的运算符现已弃用。请使用基于符号的运算符以获得更好的安全性,请阅读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)
我不知道它还能是什么
你需要关闭警告。
在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,{host:config.host,dialect:config.dialect,pool:{max:5,min:0,idle:10000},logging:false ,freezeTableName:true,operatorsAliases:false});
你在这里与Promises合作,你要么等待还是要回来。如果你的节点版本中有async await可用,你可以这样做:
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;
})
});
};