![](/img/trans.png)
[英]JS says an object is undefined even though it shows with console.log
[英]Discord.js says "message is empty" but console.log shows data
我目前正在開展一個項目,將我的 google 電子表格的信息發送到 discord 頻道。 所以,我有一些問題:
1)最終數據(值),當我嘗試console.log(value)
時,它起作用了!
所以這意味着它確實從 google-spreadsheets 獲取數據並將其設置為(值)。
但是當我嘗試執行msg.channel.send(value)
時,它以崩潰響應我並說不能發送空消息這讓我感到困惑,為什么?
因此我無法將此值發送到我的 discord 頻道。
2)我的代碼底部顯示的最終數據(值)實際上是這樣的數組:
{ values: [ [A] , [B] , [C] , [D] , ... ] }
而且我不知道如何將它們更改為A,B,C,D
形式。 有誰知道如何轉換它們?
const { google } = require('googleapis')
async function google_sheet_get() {
const auth = new google.auth.GoogleAuth({
keyFile: "./google_sheets/Google_credentials.json",
scopes: "https://www.googleapis.com/auth/spreadsheets"
})
const client = await auth.getClient()
const googleSheets = google.sheets({version: "v4", auth: client})
const spreadsheetId = "My Sheet ID"
const getRows = await googleSheets.spreadsheets.values.get({
auth,
spreadsheetId,
majorDimension: "ROWS",
range: "'Day1'!B1:F24"
})
return getRows.data
}
client.on('messageCreate', msg=> {
if(msg.content.startsWith(settings.prefix+'ttes')||
msg.content.startsWith(settings.prefix2+'ttes')) {
sheet.get().then( value => msg.channel.send(value))
msg.channel.send("Command ttes success.")
}
})
真的很感謝幫助,謝謝。
我猜該value
不是字符串,當您查看channel.send()時,它接受字符串“MessagePayload”object 或“MessageOptions”object,因此如果該value
object,它會嘗試將其解釋為其中之一失敗,因為所需的屬性不存在。
在發送之前嘗試將其轉換為字符串,例如: msg.channel.send(`${value}`)
或msg.channel.send(value.toString()
。
至於問題的第二部分,您可能想看看Array.flat() 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.