2

I'm a junior in js development so this might be a silly question, so apologies.

I'm using firebase in my app to store my strings, I call it once when the app is loaded and keep it in memory for faster access.

Then I use strings.find to find a specific string in the array. All work find when running on iOS, but when running on Android I keep getting this weird error

TypeError: Undefined is not a function (evaluting 'this.strings.find...

Here's my data schema

{'strings': [{'name':'something', 'value':'something'} ... ]

And here's my code

getString(name) { return this.strings.find(x => x.name == name).value }

And this is where I define the object

init(onUpdated) {
    Promise.all([stringsDb.once('value'), multiLang.once('value')])
      .then(([stringsSnapshot, multiLangSnapshot]) => {

        this.strings = stringsSnapshot.value
        this.multiLang = multiLangSnapshot.value

        onUpdated(true)
      })
      .catch((err) => {
        console.log(err.stack)
        onUpdated(false)
      });
  }
4
  • 1
    Where are you setting this.strings? Commented Jun 30, 2017 at 7:16
  • @AndrewLi Thanks! I edited the question Commented Jun 30, 2017 at 7:25
  • my guess is (because it's asynchronous) this.strings undefined before the promise is resolved and this.strings is assigned to. Call getName after this.strings is set or check to see if it's not undefined before performing find. Commented Jun 30, 2017 at 7:27
  • I thought that, so I printed this.strings to the console, and it is defined already (I use callbacks to only call the find after the strings is already defined) Commented Jun 30, 2017 at 7:28

1 Answer 1

3

This probably happens because the this.strings is not an array. you need to debug it and see if its actually an array or not, my guess is it returns an object, if so then you need to use a reducer or perhaps Object.values or something to convert to an array.

p.s you should use the === operator when comparing the strings

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

1 Comment

Thank you!! That's was exactly it. Turns out firebase for Android doesn't return the same object as for iOS, so I'll have to convert it to arrive before using

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.