当我尝试与数据交互时,会引发错误.
const user = client.users.cache.get(user.id);用户.发送(消息);
<块引用>
TypeError: 无法读取未定义的属性发送"
缓存工具说明
缓存是一个Collection,是Collection的扩展Map 但带有 array 方法等等.主要供managers使用,用来防止无用API 调用,当数据被获取时,它也将被缓存,您将能够在不发送 API 请求的情况下检索它多少次.因此,当数据没有被缓存时,这意味着数据还没有被提取,你需要这样做.
请求是对 Discord API 的调用,由 Discord.js 模块,等待 Discord 服务器的响应,Promise 需要 awaited 带有 await
关键字或 <Promise>.then
方法.
获取示例
您可以获取数据并将响应分配到变量中,当您想再次访问它时,您将能够在缓存中检索数据.
/* 未获取数据,因此尚未缓存 */console.log(client.users.cache.get(user.id));//不明确的/* 从 Discord 请求数据 */const fetchedData = 等待 client.users.fetch(user.id);console.log(fetchedData);//用户 {}/* 数据已被获取,因此您可以从缓存中检索数据 */console.log(client.users.cache.get(user.id));//用户 {}
经过这些解释,出现此错误是正常的,因为无法将方法应用于undefined
.
TypeError: 无法读取未定义的属性 ''
When I try to interact with data an error is thrown.
const user = client.users.cache.get(user.id);
user.send(message);
TypeError: Cannot read property 'send' of undefined
Explanation of cache utility
Cache is a Collection, an extend of Map but with array methods and more. It's mainly used by managers and used to prevents useless API calls, when data is fetched it will be cached too and you will be able to retrieve it how many time you want without sending API request. So when data is not cached it means that the data was not fetched yet and you'll need to.
A request is a call to the Discord API which is done by the Discord.js module, to wait the response of the Discord server, the Promise need to be awaited with await
keyword or <Promise>.then
method.
Fetch example
You can fetch data and assign the response in a variable, when you want to access it another time you will be able to retrieve data in the cache.
/* Data wasn't fetched so not cached yet */
console.log(client.users.cache.get(user.id)); // undefined
/* Request data from Discord */
const fetchedData = await client.users.fetch(user.id);
console.log(fetchedData); // User {}
/* Data has been fetched so you can retrieve data from cache */
console.log(client.users.cache.get(user.id)); // User {}
After these explanations, it's normal that this error appears since it's not possible to apply a method on something undefined
.
TypeError: Cannot read property '' of undefined
这篇关于如何获取未缓存的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!