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'
}
},
computed: {
async tableData () {
var dbData = await fetchData()
return dbData
}
}
})
Hope you can teach me the right way to fetch my data to my Vue instance!
Welcome to SO!
Computed properties are not meant to be async.
The usual pattern to address async retrieval of data is to:
data placeholdercreated or mounteddata with the new content.data in your template.In your case, you would:
tableData into an internal data, like your tableHeadcouch.fetchData function in created hook.then chain (or after awaiting), assign your tableData with the resulttableData in your template (nothing to change)See for example the Vue cookbook to consume APIs with Axios
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With