問題描述
當(dāng)我嘗試與數(shù)據(jù)交互時(shí),會(huì)引發(fā)錯(cuò)誤.
const user = client.users.cache.get(user.id);用戶.發(fā)送(消息);
<塊引用>
TypeError: 無法讀取未定義的屬性發(fā)送"
緩存工具說明
緩存是一個(gè)Collection,是Collection的擴(kuò)展Map 但帶有 array 方法等等.主要供managers使用,用來防止無用API 調(diào)用,當(dāng)數(shù)據(jù)被獲取時(shí),它也將被緩存,您將能夠在不發(fā)送 API 請(qǐng)求的情況下檢索它多少次.因此,當(dāng)數(shù)據(jù)沒有被緩存時(shí),這意味著數(shù)據(jù)還沒有被提取,你需要這樣做.
請(qǐng)求是對(duì) Discord API 的調(diào)用,由 Discord.js 模塊,等待 Discord 服務(wù)器的響應(yīng),Promise 需要 awaited 帶有 await
關(guān)鍵字或 <Promise>.then
方法.
獲取示例
您可以獲取數(shù)據(jù)并將響應(yīng)分配到變量中,當(dāng)您想再次訪問它時(shí),您將能夠在緩存中檢索數(shù)據(jù).
/* 未獲取數(shù)據(jù),因此尚未緩存 */console.log(client.users.cache.get(user.id));//不明確的/* 從 Discord 請(qǐng)求數(shù)據(jù) */const fetchedData = 等待 client.users.fetch(user.id);console.log(fetchedData);//用戶 {}/* 數(shù)據(jù)已被獲取,因此您可以從緩存中檢索數(shù)據(jù) */console.log(client.users.cache.get(user.id));//用戶 {}
經(jīng)過這些解釋,出現(xiàn)此錯(cuò)誤是正常的,因?yàn)闊o法將方法應(yīng)用于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
這篇關(guān)于如何獲取未緩存的數(shù)據(jù)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!