簡體   English   中英

類型錯誤:db.collection 不是函數(Firestore 和 Discord.js)

[英]TypeError: db.collection is not a function (Firestore and Discord.js)

我正在嘗試利用 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');

任何幫助是極大的贊賞! 謝謝!

你的論點順序錯誤。 它們是這樣聲明的:

async (client, message, args, db)

但是這樣過去了:

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

dbargs交換。

暫無
暫無

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

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