V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xuyl
V2EX  ›  Node.js

mongoose 查询问题求教

  •  
  •   xuyl · 2018-10-30 09:06:22 +08:00 · 2836 次点击
    这是一个创建于 1976 天前的主题,其中的信息可能已经有所发展或是发生改变。

    主函数如下, 其中 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 属性?

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

    第 1 条附言  ·  2018-10-30 09:51:45 +08:00
    问题已解决,多谢各位耐心回答。还要把 promise 这一块多理解下,打牢点基础。V 友们集思广益,又涨了不少姿势。
    11 条回复    2018-10-30 09:53:45 +08:00
    abcdGJJ
        1
    abcdGJJ  
       2018-10-30 09:19:06 +08:00 via Android
    把 map 换成 for 循环试试,好像 map 里的异步函数会有问题
    ilaipi
        2
    ilaipi  
       2018-10-30 09:25:39 +08:00
    #1 说的是对的,要改成 for,map 这种是回调函数,async 没用。另外你可以去了解一下 populate,看你的需求大概能一次查询出来。
    licoycn
        3
    licoycn  
       2018-10-30 09:28:44 +08:00
    兄弟 撞头了
    gyteng
        4
    gyteng  
       2018-10-30 09:32:01 +08:00
    map 不能这么用,非得这样用的话,先 map 出一个 promise 数组,再 Promise.all 调用出结果
    xuyl
        5
    xuyl  
    OP
       2018-10-30 09:33:29 +08:00
    @abcdGJJ @ilaipi 感谢,已解决。由于分类层级较多,并且有重名的,populate 似乎不太好操作
    @licoycn 不懂什么意思。
    licoycn
        6
    licoycn  
       2018-10-30 09:34:51 +08:00
    @xuyl 我们头像一样^@@@@@@@@@@@^,所以说撞头了 哈哈
    owenliang
        7
    owenliang  
       2018-10-30 09:35:38 +08:00
    async await 真牛逼。。
    meko
        8
    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
        9
    xuyl  
    OP
       2018-10-30 09:38:38 +08:00
    @licoycn 手动狗头
    fanshide
        10
    fanshide  
       2018-10-30 09:42:00 +08:00
    map 内部是不支持异步的,推荐了解下 for await of ;或者先得到一个 promise 数组,再使用 Promise.all()
    meko
        11
    meko  
       2018-10-30 09:53:45 +08:00
    @meko
    要再包一层 Promise.all
    tree = await Promise.all(tree.map(async item=>{
    ```````````````````
    }))
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1418 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:46 · PVG 07:46 · LAX 16:46 · JFK 19:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.