![](/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.