Waterline ORM (sails.js) conditions with NOT Equal in query

不想你离开。 提交于 2020-12-29 09:34:26

问题


How can I write a NOT Equal condition in Waterline?

This code:

Users
  .count()
  .where({
     id: { '!=': req.params.id},
     lastname: req.body.lastname
  })

Does nothing... (disk adapter in sails.js)


回答1:


First thing, it do nothing because looking into database is asynchronous. You have to make the chain longer and add exec or something from Q library like then.

User.count().where({
    id: { '!=': req.params.id },
    lastname: req.body.lastname
}).exec(function(err, num){
    console.log(num);
});

Now it returns 0. To make it return proper number instead of '!=' just write '!'.




回答2:


Adding this answer for anyone using Postgres. I was trying this and banging my head against a wall as it wasn't working and I couldn't figure out why. I searched all over and kept finding this answer.

If you are on postgres you want to use

id: { not: req.params.id }

After looking through Sailsjs docs and searching google long and hard I found this answer in the comments of the sails-postgresql module query.js. Hopefully this helps save someone in the same situation some time. Here is the full comment block that saved my sanity.

/**
 * Specifiy a `where` condition
 *
 * `Where` conditions may use key/value model attributes for simple query
 * look ups as well as more complex conditions.
 *
 * The following conditions are supported along with simple criteria:
 *
 *   Conditions:
 *     [And, Or, Like, Not]
 *
 *   Criteria Operators:
 *     [<, <=, >, >=, !]
 *
 *   Criteria Helpers:
 *     [lessThan, lessThanOrEqual, greaterThan, greaterThanOrEqual, not, like, contains, startsWith, endsWith]
 *
 * ####Example
 *
 *   where: {
 *     name: 'foo',
 *     age: {
 *       '>': 25
 *     },
 *     like: {
 *       name: '%foo%'
 *     },
 *     or: [
 *       { like: { foo: '%foo%' } },
 *       { like: { bar: '%bar%' } }
 *     ],
 *     name: [ 'foo', 'bar;, 'baz' ],
 *     age: {
 *       not: 40
 *     }
 *   }
 */


来源:https://stackoverflow.com/questions/20378997/waterline-orm-sails-js-conditions-with-not-equal-in-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!