2

I'm trying to do a search using Firebase. I have this in my VueJS Code.

export default {
  data () {
    return {
      listings: [],
      searchData: {
        keyword: ""
      }
    }
  },
  name: 'SearchScreen',
  components: {
    ValidationProvider,
    ValidationObserver
  },
  firebase: {
    listings: listingsRef
  },
  methods: {
    search () {
      console.log(this.searchData.keyword)
      listingsRef.orderByChild('location').equalTo(this.searchData.keyword).on('value', function (snapshot){
        console.log(snapshot.val())
        return{
          listings: snapshot.val()
        }
      })
    }
  }
}

Now when I do the console.log It successfully filters out data and shows the response in the console. But I couldn't update the 'listings' in component data with the response I got from Firebase. Tried this.listing but didn't work. How can I do this?

4
  • What does console.log(snapshot.val()) returns? array or object? Commented Apr 20, 2020 at 13:53
  • @palash Its an array of objects Commented Apr 20, 2020 at 13:59
  • 1
    What is console.log( this ) inside search () at start and inside .on('value', function (snapshot){ ... ? Please post the images in your main post. Commented Apr 20, 2020 at 14:14
  • @palaѕн I solved it. (snapshot)=>{} did the work. Thanks Commented Apr 20, 2020 at 15:11

3 Answers 3

3

You should assign this (component instance) to a global variable vm and then in the callback assign that snapshot.val() to the listing data property as follows :

 search () {
      console.log(this.searchData.keyword)
       let vm =this;
      listingsRef.orderByChild('location').equalTo(this.searchData.keyword).on('value', function (snapshot){
        console.log(snapshot.val())

          vm.listings= snapshot.val()

      })
    }

or use the callback as an an arrow function (snapshot)=>{} :

 search () {
      console.log(this.searchData.keyword)
      listingsRef.orderByChild('location').equalTo(this.searchData.keyword).on('value',  (snapshot)=>{
        console.log(snapshot.val())

          this.listings= snapshot.val()

      })
    }

Sign up to request clarification or add additional context in comments.

6 Comments

I tried but it says Cannot set property 'listings' of null
of null or undefined ? did you add let vm =this;?
@SuvinNimnakaSukka This is not possible, as vm here refers to the vue instance.
@Boussadjra null . Yes I added that too
@palaѕн what do you mean?
|
1

One way to do that is to bind this to your function:

listingsRef.orderByChild('location').equalTo(this.searchData.keyword).on('value', function (snapshot){
    this.listings = snapshot.val()
}.bind(this))

Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

2 Comments

Didnt work :( But @Boussadjra gave a solution with an arrow function and it worked. Thanks
Ok! Nice to know! :) Don't forget to upvote @Boussadjra 's answer and accept it as the correct answer to your question.
-1

to the search function add a reference to the component

thisComponent = this

then inside the function (snapshot)

use thisComponent.searchData.keyword = (whatever)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.