简体   繁体   English

为什么当我在记录项目时知道事实时 Map.get() 返回 undefined?

[英]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:验证是否是这种情况的最简单测试是将setget调用的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.

相关问题 打字稿map.get返回未定义 - Typescript map.get return undefined JavaScript Map.get(variableName) 返回 undefined - JavaScript Map.get(variableName) is returning undefined 为什么当我请求 Angular 中的项目时服务返回未定义? - Why does the service return me undefined when I request an item in Angular? 当我在this.state中填充数组时,为什么它返回未定义? - When I map over a populated array in this.state, why does it return undefined? 当我尝试将后端与 React 连接时,为什么它会返回“无法读取未定义的属性“地图”? - Why does it return `Cannot read property 'map' of undefined` when I'm trying to connect backend with React? 当我返回`false`时,为什么`_. map`返回undefined? - Why is `_.map` returning undefined when I return `false`? 从 mongodb 获取数据时,节点 js javascript 中的 Map.get() 总是返回 undefined - Map.get() in node js javascript always return undefined while fetching data from mongodb 在 typescript 中使用 Map.get() 方法,返回 undefined,而我正在处理 undefined - Using Map.get() method in typescript, returns undefined, while I am handling undefined 使用puppeteer时,为什么getElementsByClassName()返回未定义? - Why does get getElementsByClassName() return undefined, when using puppeteer? 为什么当我尝试在 react.js 中获取图像的高度和宽度时它返回未定义 - Why does it return undefined when I try to get the height and width of an image in react.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM