javascript - 如何根据连接表的列对 Bookshelf.js 查询的结果进行排序?

标签 javascript node.js bookshelf.js

我试图从 Bookshelf.js 查询中获取结果,并按连接表中的列对它们进行排序。但是,当添加“withRelated”时,我只能获取目标表,而不能获取连接表本身。下面是我到目前为止的代码。

MyTable.forge().query({where: {id: req.params.id}})
  .fetch({
    withRelated: [
      {'children.children': (qb) => { qb.orderBy('position', 'asc'); }}
    ]
  }).then(result => {
    res.send(JSON.stringify({theResult: result}));
  });

“orderBy”应该按仅在连接表中的“position”列进行排序。看来“qb”只返回目标表的结果,而不返回连接表的结果。我尝试过“withPivot”,但没有得到正确的结果。

最佳答案

因此,在查看返回的对象并阅读更多文档后,我发现 orderBy 的属性被 Bookshelf.js 更改为“_pivot_position”。 “pivot”被添加到列名称中,这就是您想要使用的。然而,这似乎只适用于以下情况。

withRelated: [
      {'children': (qb) => { qb.orderBy('_pivot_position', 'asc'); }}
    ]

如上所示,我必须排除“children.children”并仅使用“children”。我现在想知道 Bookshelf.js 是否允许您从子元素指定多个“orderBy”列。

关于javascript - 如何根据连接表的列对 Bookshelf.js 查询的结果进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45000365/

相关文章:

node.js - Bookshelf.js 在创建新记录之前进行独特的电子邮件验证

javascript - 通过跨站点 AJAX 解释 JSONP

node.js/karma/end-to-end testing : failed to proxy/app/index. html(错误:connect ECONNREFUSED)

Javascript:为什么对象在新的 api 调用时没有得到初始化,而字符串变量却得到了初始化?

node.js - 在 mongoose 中使用 mongodb 多键索引方法索引引用数组

node.js - 如何从 Firebase 向 node.js 发送通知?

javascript - Bookshelf js fetch withRelated选项返回空关系对象

javascript - 为每个 Iframe 生成唯一的 Id,或从 DOM 节点构建 Iframe

javascript - Visual Studio Code 找不到模块 'typegram/callback'

javascript:捕获当前页面上的任何点击事件并阻止导航?