link2466 link2467 link2468 link2469 link2470 link2471 link2472 link2473 link2474 link2475 link2476 link2477 link2478 link2479 link2480 link2481 link2482 link2483 link2484 link2485 link2486 link2487 link2488 link2489 link2490 link2491 link2492 link2493 link2494 link2495 link2496 link2497 link2498 link2499 link2500 link2501 link2502 link2503 link2504 link2505 link2506 link2507 link2508 link2509 link2510 link2511 link2512 link2513 link2514 link2515 link2516 link2517 link2518 link2519 link2520 link2521 link2522 link2523 link2524 link2525 link2526 link2527 link2528 link2529 link2530 link2531 link2532 link2533 link2534 link2535 link2536 link2537 link2538 link2539 link2540 link2541 link2542 link2543 link2544 link2545 link2546 link2547 link2548 link2549 link2550 link2551 link2552 link2553 link2554 link2555 link2556 link2557 link2558 link2559 link2560 link2561 link2562 link2563 link2564 link2565 link2566 link2567 link2568 link2569 link2570 link2571 link2572 link2573 link2574 link2575 link2576 link2577 link2578 link2579 link2580 link2581 link2582 link2583 link2584 link2585 link2586 link2587 link2588 link2589 link2590 link2591 link2592 link2593 link2594 link2595 link2596 link2597 link2598 link2599 link2600 link2601 link2602

[Vue.js] Async data fetching not updating reactive data property

Ok, guys, Im having a little issue today, all day long, trying to solve, the deal goes like this…

Im fetching some data from firebase to render on the html template with asynchronous functions

there is a fetchList Method that is like this:

async mounted() {
let ret = await this.fetchJobRequireList()
console.log(‘fetchjoblist’ , ret)

async fetchJobRequireList() {
// debugger
let services = JSON.parse(sessionStorage.getItem(‘required_services’))
services != null ? this.required_services = services : null

let docs_ = []
let result = []
if (!services) {
// this.required_services = []
// get required services per user id
let collections = this.$options.firebase.functions().httpsCallable(‘getRequiredServices’)

let docs = await this.$options.firebase.firestore().collection(‘required_services’).get()
// console.log(‘required services docs’, docs)

let _ = this
for (let doc of docs.docs) {

result[doc.id] =
await collections({doc_id: doc.id}).then( async r => {
// debugger
let collections_ = r.data.cols
docs_ = []
_.required_services[doc.id] = []
for (let collection of collections_) {
let path = collection._referencePath.segments

// let documents =
let __ = _
await this.$options.firebase.firestore().collection(path[0])
.doc(path[1]).collection(path[2]).get()
.then(async documents => {
// console.log(‘__documents__‘, documents)
for (let doc_ of documents.docs) {
doc_ = await documents.docs[0].ref.get()
doc_ = {
id: doc_.id,
path: doc_.ref.path,
data: doc_.data()
}
// debugger
__.required_services[doc.id].push(doc_)
console.log(“this?”, this.required_services[doc.id], ‘__??’, __.required_services)
docs_.push(doc_)
}
})
}
console.log(‘__docs__‘, docs_)

return docs_
}).catch(err => console.error(err))

// console.log(‘this.required_services’, this.required_services)
}

}
// console.log(‘object entries’, Object.entries(result))
// console.log(‘__this.required_services__‘, Object.entries(this.required_services))
// sessionStorage.setItem(‘required_services’, JSON.stringify(this.required_services))
return result

}

The expected result would be for the data function properties to be update after the firebase response came, but no update is happening.

If anyone, have any clues, of what could be happening… some people told me that asynchrounous functions could cause problems… but there is no alternative for them, I guess…

Solution :

This line

_.required_services[doc.id] = []

is not reactive. See the first point in the docs

Solution 2:

So as pointed by @StephenThomas, there is some limitations in array change detection capabilities in reactive property usage.
So after loading the content from firebase, try to push it like this.joblist.push(doc) vue.js property will not react properly and make some confusion in the head of someone that doesnt know about this limitation in detecting this kind of array mutation (https://vuejs.org/v2/guide/list.html#Caveats)...
By using this line, now is possible to see the changes in property inside the vue.js dev tools

_.joblist.splice(0,0, local_doc)

Thanks @SthephenThomas, for pointing this out!!