V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
xuyl
V2EX  ›  Node.js

mongoose 查询问题求教

  •  
  •   xuyl · Oct 30, 2018 · 3900 views
    This topic created in 2746 days ago, the information mentioned may be changed or developed.

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

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

    Supplement 1  ·  Oct 30, 2018
    问题已解决,多谢各位耐心回答。还要把 promise 这一块多理解下,打牢点基础。V 友们集思广益,又涨了不少姿势。
    11 replies    2018-10-30 09:53:45 +08:00
    abcdGJJ
        1
    abcdGJJ  
       Oct 30, 2018 via Android
    把 map 换成 for 循环试试,好像 map 里的异步函数会有问题
    ilaipi
        2
    ilaipi  
       Oct 30, 2018
    #1 说的是对的,要改成 for,map 这种是回调函数,async 没用。另外你可以去了解一下 populate,看你的需求大概能一次查询出来。
    licoycn
        3
    licoycn  
       Oct 30, 2018
    兄弟 撞头了
    gyteng
        4
    gyteng  
       Oct 30, 2018
    map 不能这么用,非得这样用的话,先 map 出一个 promise 数组,再 Promise.all 调用出结果
    xuyl
        5
    xuyl  
    OP
       Oct 30, 2018
    @abcdGJJ @ilaipi 感谢,已解决。由于分类层级较多,并且有重名的,populate 似乎不太好操作
    @licoycn 不懂什么意思。
    licoycn
        6
    licoycn  
       Oct 30, 2018
    @xuyl 我们头像一样^@@@@@@@@@@@^,所以说撞头了 哈哈
    owenliang
        7
    owenliang  
       Oct 30, 2018
    async await 真牛逼。。
    meko
        8
    meko  
       Oct 30, 2018
    ```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
       Oct 30, 2018
    @licoycn 手动狗头
    fanshide
        10
    fanshide  
       Oct 30, 2018
    map 内部是不支持异步的,推荐了解下 for await of ;或者先得到一个 promise 数组,再使用 Promise.all()
    meko
        11
    meko  
       Oct 30, 2018
    @meko
    要再包一层 Promise.all
    tree = await Promise.all(tree.map(async item=>{
    ```````````````````
    }))
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5640 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 94ms · UTC 07:36 · PVG 15:36 · LAX 00:36 · JFK 03:36
    ♥ Do have faith in what you're doing.