【问题标题】:Waterline ORM (sails.js) conditions with NOT Equal in query查询中不等于的 Waterline ORM (sails.js) 条件
【发布时间】:2013-12-21 03:41:33
【问题描述】:

如何在 Waterline 中编写 NOT Equal 条件?

这段代码:

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

什么都不做...(sails.js 中的磁盘适配器)

【问题讨论】:

    标签: javascript node.js orm sails.js waterline


    【解决方案1】:

    首先,它什么都不做,因为查看数据库是异步的。您必须使链更长,然后添加 exec 或 Q 库中的其他内容。

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

    现在它返回 0。要让它返回正确的数字而不是 '!=',只需写 '!'。

    【讨论】:

      【解决方案2】:

      为使用 Postgres 的任何人添加此答案。我正在尝试这个并将我的头撞在墙上,因为它不起作用,我不知道为什么。我到处搜索并不断找到这个答案。

      如果你在 postgres 你想使用

      id: { not: req.params.id }
      

      在查看 Sailsjs 文档并在谷歌上长时间地搜索后,我在sails-postgresql 模块 query.js 的 cmets 中找到了这个答案。希望这有助于在一段时间内拯救处于相同情况的人。这是完整的评论块,让我保持清醒。

      /**
       * 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
       *     }
       *   }
       */
      

      【讨论】:

        【解决方案3】:

        我没有直接用count,我用length知道count。

        let user = User.find({
           where: {
            id: { '!=': req.params.id },
            lastname: req.body.lastname
          }
        });
        let count = user.length;
        

        【讨论】:

          猜你喜欢
          • 2014-01-01
          • 2018-09-26
          • 1970-01-01
          • 2015-01-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多