简体   繁体   English

离子存储将预期的数组作为字符串返回

[英]Ionic storage is returning expected array as a string

I'm storing ID's in the storage.我将 ID 存储在存储器中。

See example:参见示例: 在此处输入图像描述

I store the storage to a local string [];我将存储存储到本地字符串[];

The problem is when I try to push new ids to storage.问题是当我尝试将新 ID 推送到存储时。

 addIdToDontShowList(id: string): Promise<any> { return this.storage.get(DONT_SHOW).then((ids: string[]) => { if (ids) { console.log('ids', ids); ids.push(id); return this.storage.set(DONT_SHOW, ids); } else { return this.storage.set(DONT_SHOW, id); } }); }

I get an error that the method push does not exist on IDs.我收到一个错误消息,指出 ID 上不存在推送方法。

ERROR Error: Uncaught (in promise): TypeError: ids.push is not a function TypeError: ids.push is not a function错误错误:未捕获(承诺):类型错误:ids.push 不是函数类型错误:ids.push 不是函数

How do I convert ids to a string [] so that I can push new items to ids before setting the storage again?如何将 ids 转换为字符串 [] 以便我可以在再次设置存储之前将新项目推送到 ids?

I fixed this by making my initial commit to storage and array.我通过对存储和阵列进行初始提交来解决此问题。

Instead of return this.storage.set(DONT_SHOW, id);而不是返回 this.storage.set(DONT_SHOW, id); I stored the id into and array and set it using the array.我将 id 存储到数组中并使用数组进行设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM