首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在promise中使用promise?

如何在promise中使用promise?
EN

Stack Overflow用户
提问于 2020-07-25 07:13:05
回答 3查看 49关注 0票数 0

我正在尝试生成一个伪XML与汽车品牌和型号。但是我得到了一个错误

ReferenceError:未定义模型。那是因为这是一个承诺吗?这样做的正确方法是什么?谢谢

代码语言:javascript
复制
const output = () => {

    const id = 1

    brand(id)
        .then((brand) => {
            const models = models(brand.id)

            let xml = '<brand>';
            models.map((model) => {
                xml += '<brand>' + model.name + '</brand>';
            });
            xml += '</brand>';
            return response.send(xml);
        })
});

const brand = (id) => {
    return database
        .collection("brands")
        .doc(id)
        .get();
};

const models = (brandId) => {
    return database
        .collection("brands")
        .doc(brandId)
        .collection("models")
        .get();
};
EN

回答 3

Stack Overflow用户

发布于 2020-07-25 07:20:29

有几个问题。首先,您在定义models之前就在使用它,这会导致问题。其次,模型确实是一个承诺,所以您不能将其直接赋值给变量。为此,我建议使用async/await

代码语言:javascript
复制
const brand = (id) => {
    return database
        .collection("brands")
        .doc(id)
        .get();
};

const models = (brandId) => {
    return database
        .collection("brands")
        .doc(brandId)
        .collection("models")
        .get();
};
const output = async () => {

    const id = 1

    const brand = await brand(id);
        
    const models = await models(brand.id)

     let xml = '<brand>';
     models.map((model) => { xml += '<brand>' + model.name + '</brand>'; });
     xml += '</brand>';
     return response.send(xml);
});

此外,这里没有定义响应,但我猜您在其他地方有它。否则这也会失败

票数 2
EN

Stack Overflow用户

发布于 2020-07-25 07:20:59

您还需要解析模型承诺。我还会重命名您的方法,以避免名称冲突。请参见示例:

代码语言:javascript
复制
const output = () => {

    const id = 1

    getBrand(id)
        .then((brand) => {
            return getModels(brand.id)
                .then(modules => {
                    let xml = '<brand>';
                    models.map((model) => {
                        xml += '<brand>' + model.name + '</brand>';
                    });
                    xml += '</brand>';
                    return response.send(xml);
                });

        })
});

const getBrand = (id) => {
    return database
        .collection("brands")
        .doc(id)
        .get();
};

const getModels = (brandId) => {
    return database
        .collection("brands")
        .doc(brandId)
        .collection("models")
        .get();
};
票数 1
EN

Stack Overflow用户

发布于 2020-07-25 07:20:17

Ciao,你也应该为models调用.then,就像这样:

代码语言:javascript
复制
const output = () => {

const id = 1

brand(id)
    .then((brand) => {
        models(brand.id)
           .then((models) => {
              let xml = '<brand>';
              models.map((model) => {
                 xml += '<brand>' + model.name + '</brand>';
              });
              xml += '</brand>';
              return response.send(xml);
        })           
    })
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63082576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档