[英]Why does Map.get() return undefined when I know for a fact when logged the item is there?
I'm curious if I'm missing something here -- When I call a method that should get something from the collection because I have proof it exists, it doesn't get it still My Object of Collections我很好奇我是否在这里遗漏了一些东西——当我调用一个应该从集合中获取某些东西的方法时,因为我有证据证明它存在,但它仍然没有得到它
export const databaseCache = {
/* Other collections ommitted */
roles: new Collection<string, Role>(),
};
My method我的方法
export function getRoleByName(name: string) {
return databaseCache.roles.get(name);
}
and my call和我的电话
const feedbackMod = getRoleByName("pack feedback helper");
feedbackMod is undefined even though I have proof it exists because I logged the collection, (collection is considered a Map object as Collection is a class that extends Map) feedbackMod 是未定义的,即使我有证据证明它存在,因为我记录了集合,(集合被认为是 Map object 因为集合是扩展 Map 的 class)
https://imgur.com/hD2RP2S image of proof of collection item existing https://imgur.com/hD2RP2S现有收藏品证明图片
Reproduceable Code:可重现的代码:
interface Role {
name: string;
roleId: number;
}
const myCollections = {
roles: new Map<string, Role>()
};
myCollections.roles.set("pack feedback helper", { name: "pack feedback helper", roleId: 543576460363956244 });
const item = myCollections.roles.get("pack feedback helper");
console.log(item);
Although when tested online, this works, locally it does not for me.虽然在网上测试时,这是可行的,但在本地它不适合我。
Almost certainly there is something more going on that your example shows.几乎可以肯定,您的示例显示了更多的事情。 The likely problem is that you have a String
vs. a string
kicking around on one side or the other (eg an objectified
string vs. a primitive
string):可能的问题是你有一个String
与一个在一侧或另一侧踢的string
(例如,一个objectified
字符串与一个primitive
字符串):
let key = "pack feedback helper";
let memo = new Map();
memo.set(key, "some value here");
memo.get(key); // Works
memo.get("pack feedback helper"); // Works
let objectifiedKey = new String(key);
memo.get(objectifiedKey); // DOES NOT WORK
The simplest test to validate that this is the case is to wrap both the set
and the get
calls' key
argument in String
to de-objectify the key:验证是否是这种情况的最简单测试是将set
和get
调用的key
参数包装在String
中以对 key 进行反对象化:
// Continuing from the example above
let deObjectifiedKey = String(objectifiedKey); // Note the lack of `new` here
memo.get(deObjectifiedKey); // WORKS AGAIN!
If it is the case that de-objectifying works, then you can trace which side is causing the problem by checking typeof key
.如果是去对象化的情况,那么您可以通过检查typeof key
来追踪导致问题的一方。 It will be object
for "objectified" values and string
for primitive ones.对于“对象化”值,它将是object
,对于原始值,它将是string
。
Alternatively, test in another browser.或者,在另一个浏览器中测试。 If it works in one and not another, then you may have a browser bug.如果它适用于一个而不是另一个,那么您可能有一个浏览器错误。
I found my answer;我找到了答案; it is too much code to paste but in short and simple terms it was because some of my methods were lexically scoped, thus being read by the compiler first, where the collection still had no items in it.粘贴的代码太多,但简而言之,这是因为我的一些方法是词法范围的,因此首先由编译器读取,其中集合中仍然没有任何项目。 When I converted the lexical scoped methods to block (es6 arrow functions), everything was able to load in correctly and be used accordingly.当我将词法范围的方法转换为块(es6 箭头函数)时,一切都能够正确加载并相应地使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.