zepp-health / zeppos-samples

A collection of samples about Zepp OS watchfaces and applications
Apache License 2.0
147 stars 53 forks source link

Fetch #14

Closed Ki11Z0n3 closed 2 years ago

Ki11Z0n3 commented 2 years ago

Hello, I'm trying to make an example of an api call but it doesn't show me anything, what am I doing wrong? The API is: https://jsonplaceholder.typicode.com/posts/1

app-side/index.js

const fetchData = async (ctx) => {
  try {
    let response = await fetch('https://jsonplaceholder.typicode.com/posts/1')

    ctx.response({
      data: { result: response },
    })

  } catch (error) {
    ctx.response({
      data: { result: 'ERROR' },
    })
  }
}

pages/index.js

fetchData() {
    messageBuilder.request({
      method: "GET_DATA",
    })
    .then(data => {
      const { result = {} } = data
      const { title } = result

      hmUI.createWidget(hmUI.widget.TEXT, {
        x: px(96),
        y: px(100),
        w: px(288),
        h: px(46),
        color: 0xffffff,
        text_size: px(36),
        align_h: h.ALIGN.CENTER_H,
        align_v: h.ALIGN.CENTER_V,
        text_style: hmUI.text_style.NONE,
        title
      })
    })
  }
mrcoon commented 2 years ago

@Ki11Z0n3 The content of the response may not be what is expected and you will need to parse it to the required format to display it correctly

    const { body } = await fetch('https://jsonplaceholder.typicode.com/posts/1')

    ctx.response({
      data: { result: {
        text: JSON.parse(body)
      } },
    })
Ki11Z0n3 commented 2 years ago

It doesn't work that way either, is there a way to debug this?

Ki11Z0n3 commented 2 years ago

It doesn't work that way either, is there a way to debug this?

It works now, the problem was that I had not given it network permissions