admin管理员组文章数量:1356413
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 badges1 Answer
Reset to default 6Wele to SO!
Computed properties are not meant to be async.
The usual pattern to address async retrieval of data is to:
- Use an internal
data
placeholder - Initiate the async request on ponent lifecycle hook
created
ormounted
- On request success, update the internal
data
with the new content. - Use the internal
data
in your template.
In your case, you would:
- Turn your
tableData
into an internaldata
, like yourtableHead
- Call your
couch.fetchData
function increated
hook. - In the function returned Promise
then
chain (or after awaiting), assign yourtableData
with the result - Use
tableData
in your template (nothing to change)
See for example the Vue cookbook to consume APIs with Axios
本文标签: javascriptComputedasync data fetchingStack Overflow
版权声明:本文标题:javascript - Computedasync data fetching - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744019618a2576977.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论