GithubHelp home page GithubHelp logo

Comments (4)

timostamm avatar timostamm commented on June 16, 2024

typeof v.at === string is very odd. How did you get the instance of StatReply - did you use StatReply.fromJson, or StatReply.fromBinary?

from protobuf-es.

ThereWeGo avatar ThereWeGo commented on June 16, 2024

typeof v.at === string is very odd. How did you get the instance of StatReply - did you use StatReply.fromJson, or StatReply.fromBinary?

no , I use fetch to get the data (vueuse -> usefetch )

export const fetchFireStats = async () => {
  return await nFetch(`/v2/firewatch/stat`, {
    onFetchError
  })
    .get()
    .json<ListStatReply>()
}

then use it like this:

// res type is  StatReply[] , which gen from proto 
let res = (await fetchFireStats()).data.value?.data ?? []
res.forEach((v) => {
          console.log(v.at.toDate()) // TypeError: v.at.toDate is not a function
          console.log(v.at.seconds) // seconds undefined
          console.log(typeof v.at) // string
          console.log(v.at) // output: 2020-12-01T03:21:57.330Z
          console.log(new Date(v.at as unknown as string).getTime())  it works
})

from protobuf-es.

timostamm avatar timostamm commented on June 16, 2024

I assume that .json() parses the response body as JSON and returns the result typed as ListStatReply.

But the type parameter is just for your convenience, it's the same as if you used .json() as ListStatReply, and it will compile just as well with .json() as Date. The last one is obviously wrong.

But the first one is actually wrong as well, because the JSON data you parsed has no toDate method. You need to hydrate your data with ListStatReply.fromJson. Here is an example:

export const fetchFireStats = async () => {
  const json = await nFetch(`/v2/firewatch/stat`, {
    onFetchError
  })
    .get()
    .json();
  return ListStatReply.fromJson(json);
}

from protobuf-es.

ThereWeGo avatar ThereWeGo commented on June 16, 2024

But the first one is actually wrong as well, because the JSON data you parsed has no toDate method. You need to hydrate your data with ListStatReply.fromJson. Here is an example:

谢谢!
thanks for you precise assumptions
as you said , it works!
i give <type> too much confidence

from protobuf-es.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.