TypeError: val.set is not a function (JS Map())
TypeError: val.set is not a function (JS Map())
我在执行 val.set(key, value)
时遇到这个问题,并在文件 vendor-es2015.js
.
中抛出类型错误 TypeError: val.set is not a function
代码(简体):
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 时,会抛出此错误。
tsconfig.json
"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.
可能看起来像这样(用 更新):
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);
});
我在执行 val.set(key, value)
时遇到这个问题,并在文件 vendor-es2015.js
.
TypeError: val.set is not a function
代码(简体):
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 时,会抛出此错误。
tsconfig.json
"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.
可能看起来像这样(用
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);
});