Updating property when promise is resolved
Updating property when promise is resolved
Expected result: data from response is displayed on modal
Actual result: data from response is not displayed on modal
Context: I'm using a form to submit data to a search function with a promise to get results from a db. When I submit, I also open a modal to display the results. The modal already opens with a property called allResults
that is empty. I'm trying to understand how to bind the data that I get from the resolved promise to the allResults
property connected to the modal. Or in other words, how to trigger a property "refresh" when promise gets resolved. When I implement either a watch
or computed
property they get called before the data is returned.
allResults
allResults
watch
computed
Properties:
data ()
return
allResults: ,
query: ''
The search function uses graphql apollo:
onSubmit ()
this.$apollo.query(
query: FIND_PEOPLE,
variables:
name: this.query
).then((response) =>
let a = response.data.given_names # array
let b = response.data.family_names # array
let c =
a.forEach(function(el)
if (!c.includes(el))
c.push(el)
)
var result = c.map(a => a.id);
b.forEach(function(el)
if (!result.includes(el.id))
c.push(el)
)
this.allResults.people =
this.allResults.people = c
this.allResults.clubs = response.data.clubs
);
The form:
<b-nav-form @submit="onSubmit">
<b-form-input size="sm" class="mr-sm-2" type="text" v-model="query" id="navbar-search-input" />
<b-button size="sm" class="my-2 my-sm-0" v-b-modal="'search-results-modal'" type="submit">Zoek</b-button>
</b-nav-form>
The modal:
<SearchResultsModal :all-results="allResults" :query="query"/>
The modal component:
<b-modal hide-footer ref="searchResultsModal" id="search-results-modal">
<p>"Searchterm: " + this.query</p>
<ul v-for="result of this.allResults.people" :key="result.id">
<li><b-link @click="selectResult('person', result.id)">result</b-link></li>
</ul>
</b-modal>
Props:
props: ['allResults', 'query'],
Additional context:
Please advise
SearchResultsModal
allResults
:all-results="allResults"
Thank you for pointing out the kebab-case improvement. The
SearchResultsModal
displays allResults
as a list of links.– Robert
Sep 11 '18 at 14:43
SearchResultsModal
allResults
So after fixing the attribute (if necessary), it still isn't working? I was actually asking what you did with the
prop
value in the component; how is it assigned or used? Please show some actual code– Phil
Sep 11 '18 at 14:44
prop
Fixing was a style issue, it still works. Added code per your request.
– Robert
Sep 11 '18 at 14:52
"it still works" <- I'm confused now... does it work or is it still broken?
– Phil
Sep 11 '18 at 14:53
1 Answer
1
This is a data initialisation and reactivity issue.
You initially set allResults
to an empty array, cool. But then you appear to be assigning it an object, indicated by your use of this.allResults.people
.
allResults
this.allResults.people
Vue wasn't aware of the people
property on allResults
so it cannot react to it.
people
allResults
Initialise your data so Vue knows about its properties
data ()
return
query: '',
allResults: // an object, not an array
people: ,
clubs: // assuming this is also an array
See https://vuejs.org/v2/guide/reactivity.html
Added information in
.then
of the search function to clarify that I already have this part solved of data initialisation & reactivity.– Robert
Sep 11 '18 at 15:09
.then
@Robert no you don't. You're still initialising
allResults
as an array then adding properties to it and not via Vue.set
– Phil
Sep 11 '18 at 15:10
allResults
Vue.set
@Robert I've update my answer to explain what I've been trying to point out
– Phil
Sep 11 '18 at 15:13
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I think this entirely depends on what
SearchResultsModal
does with itsallResults
prop. Also, should that be:all-results="allResults"
? See stackoverflow.com/a/52275413/283366– Phil
Sep 11 '18 at 14:32