gpt4 book ai didi

angular - TypeError : val. set is not a function (JS Map())

转载 作者:行者123 更新时间:2023-12-04 09:45:58 27 4
gpt4 key购买 nike

我在执行 val.set(key, value) 时遇到了这个问题并抛出类型错误 TypeError: val.set is not a function在文件中 vendor-es2015.js .

代码(简化):

import { Storage } from '@ionic/storage';


map = new Map();

this.storage.set('sth', map);

this.storage.get('sth').then((val) => {
val.set(key, value); //TypeError, returns { } insead of a map.
});

有趣的是,这在浏览器中运行良好,但是当它使用 Capacitor 编译到 Android 时,会抛出此错误。

配置文件
"target": "es2015",
"lib": [
"es2018",
"dom"
]

最佳答案

the documentation说:

Storage works on Strings only. However, storing JSON blobs is easy: just JSON.stringify the object before calling set, then JSON.parse the value returned from get. See the example below for more details.



可能看起来像这样(更新为 help from Bergi ):
const mapStr = JSON.stringify(Array.from(map.entries()));
this.storage.set('sth', mapStr);

this.storage.get('sth').then((val) => {
const theMap = new Map(JSON.parse(val));
theMap.set(key, value);
});

关于angular - TypeError : val. set is not a function (JS Map()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62108383/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com