簡體   English   中英

如何獲取未緩存的數據?

[英]How to fetch data which is not cached?

當我嘗試與數據交互時,會引發錯誤。

const user = client.users.cache.get(user.id);
user.send(message);

TypeError:無法讀取未定義的屬性“發送”

緩存實用程序的說明

Cache 是一個Collection ,是Map的擴展,但具有數組方法等。 它主要由 管理人員使用,用於防止無用的 API 調用,當獲取數據時,它也會被緩存,您可以在不發送 API 請求的情況下檢索它多少次。 因此,當數據未緩存時,這意味着尚未獲取數據,您需要這樣做。

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.

獲取示例

您可以獲取數據並在變量中分配響應,當您想再次訪問它時,您將能夠在緩存中檢索數據。

/* 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 {}

經過這些解釋,出現此錯誤是正常的,因為無法對undefined應用方法。

TypeError:無法讀取未定義的屬性“”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM