Hoa*_*Hoa 16 javascript node.js sequelize.js
我正试图在他们的网站上关注Sequelize教程.
我已达到以下代码行.
Project.findAll({where: ["id > ?", 25]}).success(function(projects) {
// projects will be an array of Projects having a greater id than 25
})
Run Code Online (Sandbox Code Playgroud)
如果我稍微调整它如下
Project.findAll({where: ["title like '%awe%'"]}).success(function(projects) {
for (var i=0; i<projects.length; i++) {
console.log(projects[i].title + " " + projects[i].description);
}
});
Run Code Online (Sandbox Code Playgroud)
一切正常.但是,当我尝试使搜索参数动态如下
Project.findAll({where: ["title like '%?%'", 'awe']}).success(function(projects) {
for (var i=0; i<projects.length; i++) {
console.log(projects[i].title + " " + projects[i].description);
}
});
Run Code Online (Sandbox Code Playgroud)
它不再返回任何结果.我怎样才能解决这个问题?
Jav*_*aro 24
现在在Sequelize你可以尝试这个
{ where: { columnName: { $like: '%awe%' } } }
Run Code Online (Sandbox Code Playgroud)
有关更新的语法,请参见http://docs.sequelizejs.com/en/latest/docs/querying/#operators
Pau*_*aul 18
我想你会这样做:
where: ["title like ?", '%' + 'awe' + '%']
Run Code Online (Sandbox Code Playgroud)
因此,如果您使用实际变量进行此操作,则使用:
Project.findAll({where: ["title like ?", '%' + x + '%']}).success(function(projects) {
for (var i=0; i<projects.length; i++) {
console.log(projects[i].title + " " + projects[i].description);
}
});
Run Code Online (Sandbox Code Playgroud)
请尝试此代码
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
{ where: { columnName: { [Op.like]: '%awe%' } } }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18413 次 |
| 最近记录: |