最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Computedasync data fetching - Stack Overflow

programmeradmin0浏览0评论

I'm trying to create simple Vue + CouchDB apps. With Vanilla JS this works fine but I don't get the data fetched from the database with a promise or async function to my vue instance. Here is my code:

app.html

<div id="vue-app">
  <table>
    <thead>
      <tr>
        <th>{{ tableHead.name }}</th>
        <th>{{ tableHead.lastname }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="data in tableData">
        <td>{{ data.name }}</td>
        <td>{{ data.lastname }}</td>
      </tr>
    </tbody>
  </table>
</div>

app.js

const db = new PouchDB('testdb')

const couch = {
  fetchData: async function () {
    var dbData = await db.allDocs({
        include_docs: true,
        descending: true
    }).then(function (result) {
        var objects = {}
        result.rows.forEach(function (data) {
            objects[data.id] = data.doc
        })
        return objects
    }).catch(function(err) {        
        return err
    })
    return dbData
  }
}

var app = new Vue({
    el: '#vue-app',
    data: {
        tableHead: {
            name: 'Name',
            lastname: 'Lastname'
        }
    },
    puted: {
        async tableData () {
            var dbData = await fetchData()
            return dbData
        }
    }
})

Hope you can teach me the right way to fetch my data to my Vue instance!

I'm trying to create simple Vue + CouchDB apps. With Vanilla JS this works fine but I don't get the data fetched from the database with a promise or async function to my vue instance. Here is my code:

app.html

<div id="vue-app">
  <table>
    <thead>
      <tr>
        <th>{{ tableHead.name }}</th>
        <th>{{ tableHead.lastname }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="data in tableData">
        <td>{{ data.name }}</td>
        <td>{{ data.lastname }}</td>
      </tr>
    </tbody>
  </table>
</div>

app.js

const db = new PouchDB('testdb')

const couch = {
  fetchData: async function () {
    var dbData = await db.allDocs({
        include_docs: true,
        descending: true
    }).then(function (result) {
        var objects = {}
        result.rows.forEach(function (data) {
            objects[data.id] = data.doc
        })
        return objects
    }).catch(function(err) {        
        return err
    })
    return dbData
  }
}

var app = new Vue({
    el: '#vue-app',
    data: {
        tableHead: {
            name: 'Name',
            lastname: 'Lastname'
        }
    },
    puted: {
        async tableData () {
            var dbData = await fetchData()
            return dbData
        }
    }
})

Hope you can teach me the right way to fetch my data to my Vue instance!

Share Improve this question edited Jul 19, 2018 at 15:53 Jonathan Hall 79.9k19 gold badges159 silver badges203 bronze badges asked Jul 16, 2018 at 11:58 pazzeropazzero 311 gold badge1 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Wele to SO!

Computed properties are not meant to be async.

The usual pattern to address async retrieval of data is to:

  1. Use an internal data placeholder
  2. Initiate the async request on ponent lifecycle hook created or mounted
  3. On request success, update the internal data with the new content.
  4. Use the internal data in your template.

In your case, you would:

  1. Turn your tableData into an internal data, like your tableHead
  2. Call your couch.fetchData function in created hook.
  3. In the function returned Promise then chain (or after awaiting), assign your tableData with the result
  4. Use tableData in your template (nothing to change)

See for example the Vue cookbook to consume APIs with Axios

发布评论

评论列表(0)

  1. 暂无评论