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 andthis.strings
is assigned to. CallgetName
afterthis.strings
is set or check to see if it's not undefined before performingfind
. – 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
1 Answer
Reset to default 3This 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