Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

351
Visualizações
Throwing an error TypeError: Cannot add property 0, object is not extensible

Please see the code below for reference.

export const listenToHotels = (hotelIds: string[]): Observable < Hotel[] > => {
    return new Observable < Hotel[] > ((observer) => {
        const hotels: any = [];

        hotelIds.forEach((hotelId) => {
            let roomingList: any;
            return FirestoreCollectionReference.Hotels()
                .doc(hotelId)
                .onSnapshot(
                    (doc) => {
                        roomingList = {
                            hotelId: doc.id,
                            ...doc.data()
                        }
                        as Hotel;
                        console.log(`roomingList`, roomingList);
                        hotels.push({
                            ...roomingList
                        });

                    },
                    (error) => observer.error(error)
                );
        });
        //Check for error handling
        observer.next(hotels);
        console.log('hotels', hotels);

    });
};

As you can see I am trying to run a forEach on a hotelId Array and in that firestore listener is being executed. Now I want to save the response and push that into hotels array but it gives me an error object not extensible error. The thing is observer and console.log('hotels',hotels) run first because of promise being executed at later stage.

Please let me know how can I resolve this issue.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I think you could use map instead of forEach, because you could use await with map

example of using map and await: https://flaviocopes.com/javascript-async-await-array-map/

about 4 years ago · Juan Pablo Isaza Relatório

0

Check out forkJoin. It takes an array of Observables and subscribes to them at the same time.

This example is written in Angular but the operators should be identical as long as you're using a current version of rxjs.

  • I'm starting with an array of User objects (users.mock.ts)
  • Each Object is then map to a single Observable (mapCalls)
  • I now have an array of Observables that will make HTTP calls. These calls will not be made until subscribed to
  • forkJoin is then added as a wrapper to the array
  • You subscribe to that forkJoin. All of the objects will make the call.
  • When the calls have completed, the logic inside of subscribe() will be run

So in your case:

  • Map the Hotel Ids to a variable calls. Each item is the Observable logic you posted
  • You would then run forkJoin(calls).subscribe() to make all the calls
about 4 years ago · Juan Pablo Isaza Relatório

0

export const listenToHotels = (hotelIds: string[]): Observable<Hotel[]> => {
    const hotelsObservable = hotelIds.map((hotelId) => {
        let roomingList: any;
        return new Observable<Hotel>((observerInside) => {
            FirestoreCollectionReference.Hotels()
                .doc(hotelId)
                .onSnapshot(
                    (doc) => {
                        roomingList = { hotelId: doc.id, ...doc.data() } as Hotel;
                        observerInside.next(roomingList);
                    },
                    (error) => observerInside.error(error)
                );
        });
    });
    const combinedObservable = combineLatest(hotelsObservable);
    return combinedObservable;
    //Check for error handling
};

The issue was how I was handling the chained observables(Execution of promise is delayed than a normal code because they will be put in micro-queue first). They need to be handled using zip or combineLatest but latter is much better in this use case as we need the latest values for the observables.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda