javascript - React native Array.find is not a function - Stack Overflow

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

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)
      });
  }

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)
      });
  }
Share Improve this question edited Jun 30, 2017 at 8:28 orelzion asked Jun 30, 2017 at 7:14 orelzionorelzion 2,5324 gold badges30 silver badges47 bronze badges 4
  • 1 Where are you setting this.strings? – Andrew Li Commented Jun 30, 2017 at 7:16
  • @AndrewLi Thanks! I edited the question – orelzion 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. – Andrew Li 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) – orelzion Commented Jun 30, 2017 at 7:28
Add a ment  | 

1 Answer 1

Reset to default 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 paring the strings

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745265131a4619408.html

相关推荐

  • javascript - React native Array.find is not a function - Stack Overflow

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

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信