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

154
Visualizações
Vue data() is unavailable in events when using @microsoft/fetch-event-source for Server Sent Events

I'm trying to use fetchEventSource (from @microsoft/fetch-event-source) to process Server Sent Events for streaming data to the client. It works, in that the data is returned and I can log it to the console, but when I try to update my vue data in the onmessage event, I get the error:

TypeError: Cannot set properties of undefined (setting 'messages')

Any thoughts on the best way to get this working?

Basic test template:

<template>
  <div>
    <p>
      <label>Message</label><br />
      <textarea id="messages" v-model="messages"></textarea><br />
      <button @click="getMessage">GET</button>
    </p>
  </div>
</template>

and the script:

import { fetchEventSource } from "@microsoft/fetch-event-source";

export default {
  data() {
    return {
      messages: "",
    };
  },
  mounted() {
    this.messages = "initialized"; // this works
  },
  methods: {
    getMessage() {
      fetchEventSource("url-here", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        onmessage(msg) {
          console.log("message", msg.data); // this works - data is here!
          this.messages = msg.data; // this returns the error
        },
      });
    },
  },
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can try with arrow function or like :

getMessage() {
  const self = this
  fetchEventSource("url-here", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    onmessage(msg) {
      console.log("message", msg.data); // this works - data is here!
      self.messages = msg.data; // this returns the error
    },
  });
},
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