AnswerBun.com

How to use a promise inside a promise?

Stack Overflow Asked on January 5, 2022

I´m trying to generate a pseudo XML with car brands and models. but I´m getting an error in

ReferenceError: models is not defined. is that because is a promise? what´s the correct way to do this? thank you

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();
};

3 Answers

You need to also resolve the models promise. I'd also rename your methods to avoid conflicting names. See example:

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();
};

Answered by Soc on January 5, 2022

There are a couple of issues. First, you are using models before it's defined, which will cause a problem. Second, models is indeed a promise, so you can't assign it to a variable directly. I suggest using async/await for this:

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);
});


Also, response is not defined here, but I'm guessing you have it somewhere else. Otherwise this too will fail

Answered by BravoZulu on January 5, 2022

Ciao, you should call .then also for models like:

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);
        })           
    })
});

Answered by Giovanni Esposito on January 5, 2022

Add your own answers!

Related Questions

Find a cell containing a specific date

1  Asked on December 22, 2021

       

Jeykll file to import not found or unreadable: base

1  Asked on December 22, 2021 by kyu96

       

Unable to remove event listener from document

1  Asked on December 22, 2021 by prawn

     

Invalid result adding the number of 1s in a binary number

2  Asked on December 20, 2021 by aworeham

   

How to print the elements of 3 different arrays by the order?

2  Asked on December 20, 2021 by fuzzyfiso

 

Finding string elements of an array in another array

5  Asked on December 20, 2021 by p-emt

     

How to plot several columns on the same line chart in R?

2  Asked on December 20, 2021 by user13953317

     

Is there a way to craft URLs to fill forms faster?

1  Asked on December 20, 2021 by rosalind-goh

   

infinite loop while using useEffect and redux store

0  Asked on December 20, 2021 by susanta

     

Cannot convert textfield into string

1  Asked on December 20, 2021 by anuj-amin

       

Is there multiple ways to cin vector elements?

1  Asked on December 20, 2021 by 2kbummer

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir