【问题标题】:how to fetch documents within a fetched document in promise如何在承诺的已获取文档中获取文档
【发布时间】:2018-11-19 04:21:27
【问题描述】:
{
    "status": "SUCCESS",
    "ASANA": {
        "benefits": [
            "5b185c59e7179a074bebfb13"
        ],
        "steps": [
            "5b186f46e7179a074bec04f5",
            "5b186e1be7179a074bec0402"
        ],
        "_id": "5b11a58d3d62f90c1c717913",
        "asanaName": "Shavasana",
        "asanaDescription": "useful for body relaxation",
        "__v": 0
    }
}

在这里,我获取了一个 Asana 以及如何使用该 id 获取福利集合。下面的代码是用来获取 Asanas 的。

const fetchAllAsanasDao = () => {
    return new Promise((resolve, reject) => {
        asanas
            .find({})
            .then((asanasResult) => {
                asanas
                    .find({})
                resolve(asanasResult);
            })
            .catch((error) => {
                reject(error);
            });
    });
}

【问题讨论】:

  • 这是什么?你在用猫鼬吗?该结构看起来像猫鼬的文件。如果是这样,请显示相关架构以及存储“好处”的位置。您还无缘无故地用承诺包装承诺。无论如何,请在问题中提供更多详细信息,因为这真的不清楚,如果您实际上使用的是 mongoose 和 MongoDB,那么它也会被不恰当地标记。
  • 我正在使用猫鼬。我得到了解决方案。谢谢

标签: node.js collections promise response resolve


【解决方案1】:

const fetchAllAsanasDao = ({}, {}, 查询) => { 返回新的承诺((解决,拒绝)=> { 体式 .find({}, {}, 查询) .populate({ 路径:“好处”,选择:“好处描述”,}) .populate({ 路径:'asanaResources',选择:'asanaResourcesPath' }) .exec() .then((asanasResult) => { 解决(体式结果); }) .catch((错误) => { 拒绝(错误); }); }); }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2022-01-20
    • 2019-12-28
    • 2018-09-03
    • 1970-01-01
    • 2018-07-04
    相关资源
    最近更新 更多