mongoose 查询问题求教

2018-10-30 09:06:22 +08:00
 xuyl

主函数如下, 其中 mongoose 用了 bluebird 模块实现 Promise:

/**  根据查询条件生成栏目树
 *   {pid: 0}  全栏目树
 *   {_id: ObjectId('xxxx')}  指定顶级栏目栏目树
 *   若 loop=false, 则只返回一层, 不递归遍历
**/


async function getTree(query, loop=true) {
  const tree = await Category.find(query);
  if ( loop === false )
    return tree;

  tree.map( async (item) => {
    let children = await Category.find({ status: true, pid: mongoose.Types.ObjectId(item._id) });
    item.children = children;
    // Object.assign(item, {children});
    console.dir(item);

  } );
  return tree;
} );

结果发现返回的 tree 对象数组中的对象没有 children 属性?试了 Object.assign 一样无效, 但在 map 方法里 console.dir(item)又能看到 children 属性?

或者你们遇到这种无限级分类是怎么输出栏目树的?

2886 次点击
所在节点    Node.js
11 条回复
abcdGJJ
2018-10-30 09:19:06 +08:00
把 map 换成 for 循环试试,好像 map 里的异步函数会有问题
ilaipi
2018-10-30 09:25:39 +08:00
#1 说的是对的,要改成 for,map 这种是回调函数,async 没用。另外你可以去了解一下 populate,看你的需求大概能一次查询出来。
licoycn
2018-10-30 09:28:44 +08:00
兄弟 撞头了
gyteng
2018-10-30 09:32:01 +08:00
map 不能这么用,非得这样用的话,先 map 出一个 promise 数组,再 Promise.all 调用出结果
xuyl
2018-10-30 09:33:29 +08:00
@abcdGJJ @ilaipi 感谢,已解决。由于分类层级较多,并且有重名的,populate 似乎不太好操作
@licoycn 不懂什么意思。
licoycn
2018-10-30 09:34:51 +08:00
@xuyl 我们头像一样^@@@@@@@@@@@^,所以说撞头了 哈哈
owenliang
2018-10-30 09:35:38 +08:00
async await 真牛逼。。
meko
2018-10-30 09:38:24 +08:00
```bash
async function getTree(query, loop=true) {
const tree = await Category.find(query);
if ( loop === false )
return tree;

tree = tree.map( async (item) => {
//转成 object
item = item.toObject();

let children = await Category.find({ status: true, pid: mongoose.Types.ObjectId(item._id) });
item.children = children;
console.dir(item);

return item;
} );
return tree;
} );
```
xuyl
2018-10-30 09:38:38 +08:00
@licoycn 手动狗头
fanshide
2018-10-30 09:42:00 +08:00
map 内部是不支持异步的,推荐了解下 for await of ;或者先得到一个 promise 数组,再使用 Promise.all()
meko
2018-10-30 09:53:45 +08:00
@meko
要再包一层 Promise.all
tree = await Promise.all(tree.map(async item=>{
```````````````````
}))

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/502403

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX