TypeError:db.collection不是函数(Firestore和Discord.js)

时间:2020-10-03 22:02:43

标签: javascript node.js google-cloud-firestore discord.js

我正在尝试利用Google的Firestore在Discord中存储用户生成的票证以及将来的功能。我遵循Google的文档来设置数据并将其添加到数据库。我的最初测试成功了。当我用它来存储不和谐消息中的数据时,出现以下TypeError:

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');

任何帮助将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:1)

您的参数顺序错误。它们的声明如下:

async (client, message, args, db)

但是这样通过:

command.run(client, message, db, args);

dbargs被交换。