Skip to content

Commit 22bdf45

Browse files
committed
Fix issue where AsyncStorage can't re-set unsentEventString nor unsentIdentifyKey when they are null
As part of migrating unsentEvents, we are appending this._storageSuffix to both the unsentKey and unsentIdentifyKey. When either one of these is null, AsyncStorage.setItem breaks because it expects strings. To fix this, I'm only migrating these values if they are valid and not null. It might be an overkill, but I'm also explicitly JSON.stringifying the values.
1 parent b06134c commit 22bdf45

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/amplitude-client.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -238,17 +238,27 @@ AmplitudeClient.prototype._migrateUnsentEvents = function _migrateUnsentEvents(c
238238
if (this.options.saveEvents) {
239239
var unsentEventsString = values[0];
240240
var unsentIdentifyKey = values[1];
241-
Promise.all([
242-
AsyncStorage.setItem(this.options.unsentKey + this._storageSuffix, unsentEventsString),
243-
AsyncStorage.setItem(this.options.unsentIdentifyKey + this._storageSuffix, unsentIdentifyKey),
244-
]).then(() => {
245-
Promise.all([
246-
AsyncStorage.removeItem(this.options.unsentKey),
247-
AsyncStorage.removeItem(this.options.unsentIdentifyKey),
248-
]).then(cb);
249-
}).catch((err) => {
250-
this.options.onError(err);
251-
});
241+
242+
var itemsToSet = [];
243+
var itemsToRemove = [];
244+
245+
if (!!unsentEventsString) {
246+
itemsToSet.push(AsyncStorage.setItem(this.options.unsentKey + this._storageSuffix, JSON.stringify(unsentEventsString)));
247+
itemsToRemove.push(AsyncStorage.removeItem(this.options.unsentKey));
248+
}
249+
250+
if (!!unsentIdentifyKey) {
251+
itemsToSet.push(AsyncStorage.setItem(this.options.unsentIdentifyKey + this._storageSuffix, JSON.stringify(unsentIdentifyKey)));
252+
itemsToRemove.push(AsyncStorage.removeItem(this.options.unsentIdentifyKey));
253+
}
254+
255+
if (itemsToSet.length > 0) {
256+
Promise.all(itemsToSet).then(() => {
257+
Promise.all(itemsToRemove).then(cb);
258+
}).catch((err) => {
259+
this.options.onError(err);
260+
});
261+
}
252262
}
253263
}).catch((err) => {
254264
this.options.onError(err);

0 commit comments

Comments
 (0)