【发布时间】:2020-10-03 22:02:43
【问题描述】:
我正在尝试利用 Google 的 Firestore 将用户生成的票证存储在我们的 Discord 以及未来的功能中。我按照谷歌的文档设置和添加数据到数据库。我最初的测试奏效了。当我使用它来存储来自不和谐消息的数据时,我收到以下类型错误:
TypeError: db.collection is not a function
at Object.module.exports.run (D:\Projects\support-ticket-bot\commands\add-user.js:4:21)
at Client.<anonymous> (D:\Projects\support-ticket-bot\index.js:93:11)
at Client.emit (events.js:315:20)
at MessageCreateAction.handle (D:\Projects\support-ticket-bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (D:\Projects\support-ticket-bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (D:\Projects\support-ticket-bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (D:\Projects\support-ticket-bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (D:\Projects\support-ticket-bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (D:\Projects\support-ticket-bot\node_modules\ws\lib\event-target.js:125:16)
at WebSocket.emit (events.js:315:20)
我已经回到最初的测试,它仍然有效。我已经回顾了我的步骤并比较了文件,但找不到我收到此错误的正当理由。
这是我的 index.js 文件:
const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
// firebase stuff
const firebase = require('firebase/app');
const admin = require('firebase-admin');
const serviceAccount = require('./ServiceAccount.json');
admin.initializeApp({
// eslint-disable-next-line comma-dangle
credential: admin.credential.cert(serviceAccount),
});
const db = admin.firestore();
// end firestore stuff
const client = new Discord.Client();
client.commands = new Discord.Collection();
fs.readdir('./commands/', (err, files) => {
if (err) {
console.log(err);
}
const commandFiles = files.filter((f) => f.split('.').pop() === 'js');
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.help.name, command);
}
commandFiles.forEach((f, i) => {
const command = require(`./commands/${f}`);
console.log(`${i + 1}: ${f} loaded!`);
client.commands.set(command.help.name, command);
});
});
client.once('ready', () => {
console.log('Ready!');
});
client.on('message', (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content
.slice(prefix.length)
.trim()
.split(/ +/);
const commandName = args.shift().toLowerCase();
const command =
client.commands.get(commandName) ||
client.commands.find(
(cmd) => cmd.help.aliases && cmd.help.aliases.includes(commandName)
);
if (!command) return;
if (command.help.guildOnly && message.channel.type === 'dm') {
return message.reply("I can't execute that command inside DMs!");
}
if (command.help.args && !args.length) {
let reply = `You didn't provide any arguments, ${message.author}!`;
if (command.help.usage) {
reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
}
return message.channel.send(reply);
}
try {
command.run(client, message, db, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
});
client.login(token);
还有我的 add-user.js
/* eslint-disable comma-dangle */
module.exports.run = async (client, message, args, db) => {
try {
const docRef = db.collection('users').doc('test');
await docRef.set({
test: 'Test Again',
});
} catch (error) {
console.log('Something went wrong again!');
console.log(error);
}
};
module.exports.help = {
name: 'add-user',
description: 'beta command!',
args: true,
};
// db.collection('users').doc('alovelace');
非常感谢任何帮助!谢谢!
【问题讨论】:
-
在 Stack Overflow 上,不要提供代码链接。链接可能会随着时间的推移而变坏,这导致该问题将来对其他人毫无用处。相反,将清楚地再现问题的complete, minimal code 复制到问题中,并将其格式化为代码块,以便于阅读。请务必指出导致问题的具体行。
-
这不是完整的代码。我们看不到
db的来源。这就是您用来调用collection()的东西,但在这里失败了。 -
index.js 的第 15 行是将
db分配给admin.firestore();然后将db传递到第 71 行的模块中。我还应该注意,我还有另一个模块给了我一个类似的错误,我将更新 m 原始帖子以包含它。
标签: javascript node.js google-cloud-firestore discord.js