question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Empty .value when using the setup() method in compositionApi

See original GitHub issue

Question about compositionApi and useFind

When querying my Notes service:

const { items, isPending } = useFind({ model: Note, params: { query } })
return { items }

I have a populate items on refresh and on when navigating to the route through the app

Now, when I want to filter the date:

const items2 = items.value.filter(v => v)
return { items, items2 }

items is always populated, but item2 is only populated when I navigate to the component through the app and is empty on refresh. I understand you lose the store… and it appears that the value.filter fires before the data is loaded… so I’m wondering what the point of filtering within the setup method is?

The example shows it doing this: https://vuex.feathersjs.com/composition-api.html#working-with-refs

My only solution is to only return the items RefImpl and filter in a computed property or something, which feels janky and not what the examples encourages.

Any thoughts?


Here is the component. To recap items.length = 1 always, but items2.length will show 0 on refresh of page.

<template lang="pug">
.player-score( v-if="haveLoaded" )
  v-row(justify="center")
    v-col.text-center(
      cols="10"
    )
      .text-h1 {{ items.length }}
    v-col.text-center(
      cols="10"
    )
      .text-h1 {{ items2.length }}
</template>

<script>
import { useFind } from 'feathers-vuex'
import { computed } from '@vue/composition-api'

export default {
  name: 'PlayerScore345',
  setup (props, context) {
    const { Note } = context.root.$FeathersVuex.api
    const noteParams = computed(() => { return { query: { $limit: 1 } } })
    const { items, haveLoaded } = useFind({ model: Note, params: noteParams })
    const items2 = items.value.filter(v => v)
    console.log(items, haveLoaded)
    return { items, items2, haveLoaded }
  }
}
</script>

<style lang="scss" scoped>
</style>

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
J3m5commented, Mar 18, 2021

I’d say that filtering on the ref value and returning raw value make it loose reactivity. You should make it a computed to make it works.

At first sight, it seem’s to be a problem with vue 3 reactivity.

1reaction
marshallswaincommented, Mar 19, 2021

ah thanks @philipimperato for pointing out the inconsistency in the docs. And as always, thank you @J3m5 for pitching in to see the fix!

I just pushed up a fix for the docs that wraps those two filtered arrays into computed properties.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why did I get blank (empty) content when I used async setup ...
I use async setup() in Vue.js 3, but I got my HTML content to disappear. My component template did not insert to HTML,...
Read more >
context.slots is empty in setup() #84 - vuejs/composition-api
I would say that the behavior is expected. Are there any use cases that the slots must be accessed out of render() function?...
Read more >
Vue 3.2 - Using Composition API with Script Setup
Vue 3 introduced the Composition API as a new way to work with reactive ... isNamePresent = computed(() => name.value.length > 0) function...
Read more >
Composition API: setup() - Vue.js
The setup() hook serves as the entry point for Composition API usage in components ... in the template so you do not need...
Read more >
Vue JS 3 Tutorial for Beginners #11 - The Composition API ...
Hey gang, in this Vue 3 tutorial we'll dive deeper into the Composition API. We'll see how to us props in the setup...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found