首页 > 解决方案 > Discord.js/Firestore .where() 不是函数

问题描述

我正在尝试将我的不和谐机器人与 Firestore 集成。每当我尝试运行查询时,我得到 .where 不是一个函数,我不明白为什么,因为其他一切似乎都有效。这是相关的代码。我已经在 Remove.js 的顶部尝试了 firebase 的要求,但这似乎没有任何作用。这是我认为它现在应该如何工作的想法。

  1. 我运行节点。然后它运行我的 index.js 文件。
  2. 在交互创建(即创建斜杠命令)时,它会检查命令文件,在这种情况下,它是删除命令
  3. 它调用 execute(interaction, db),其中interaction 是交互斜杠命令,db 是 index.js 中的 admin.Firestore() db 引用。我完全能够使用 get 命令(即在我尝试删除之前,第一块代码有效)
  4. 因为这是一个参考,我应该能够根据 Firestore 文档调用 .where() ,但我遇到了错误“TypeError: db.collection(...).doc(...).collection(. ..).doc(...).where 不是函数”

// Index.js
    // General Setup
    const { Client, Collection, Intents } = require('discord.js')
    const config = require('./config.json')
    const fs = require('fs')
    
    // Bot Setup
    const myIntents = new Intents();
    myIntents.add(Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS)
    const bot = new Client({intents: myIntents});
    
    // Firebase Setup
    const firebase = require('firebase/app')
    const fieldValue = require('firebase-admin').firestore.FieldValue
    const admin = require('firebase-admin')
    const serviceAccount = require('./serviceAccount.json')
    
    admin.initializeApp({
        credential: admin.credential.cert(serviceAccount)
    })
    
    let db = admin.firestore();
    
    // Command Setup
    bot.commands = new Collection();
    const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'))
    
    for (const file of commandFiles) {
        const command = require(`./commands/${file}`);
        bot.commands.set(command.data.name, command);
    }
    
    // Bot Login
    bot.once('ready', async () => {
        console.log('Wheatley is online!');
    });
    
    bot.on('interactionCreate', async interaction => {
        if (!interaction.isCommand()) {
            return
        }
    
        const command = bot.commands.get(interaction.commandName)
    
        if (!command) {
            return
        }
    
        try {
            await command.execute(interaction, db)
        } catch (error) {
            console.error(error)
            await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true})
        }
    });
    
    bot.login(config.bot_token);
    ///Remove.js
    const { SlashCommandBuilder } = require('@discordjs/builders');
    require('@firebase/firestore');
    
    module.exports = {
        data: new SlashCommandBuilder()
            .setName('remove')
            .setDescription('Removes object from collection')
            .addStringOption(option => 
                option.setName('item')
                .setDescription('Enter an item in the collection to remove')
                .setRequired(true)
            ),
        async execute(interaction, db) {
            const itemName = await interaction.options.getString('item')
            const itemToDelete = db.collection('items').doc(interaction.guildId).collection('items').doc(itemName);
            const doc = await itemToDelete.get();
            if(!doc.exists) {
                return interaction.reply({
                    content: `${itemName} does not exist in the collection. Try using /list to check for the right name.`,
                    ephemeral: true
                })
            }
            
            const ownerId = interaction.user.id
            const snapshot = db.collection('items').doc(interaction.guildId).collection('items').doc(itemName).where("ownerId", "==", ownerId).get();
            if(!snapshot.exists) {
                return interaction.reply({
                    content: `You are not the owner of ${itemName}. Please contact owner to delete this from the collection`,
                    ephemeral: true
                })
            }
            itemToDelete.delete();
            return await interaction.reply(`${itemName} was removed from the collection!`)
        },
    };

标签: node.jsfirebasegoogle-cloud-firestorediscord.js

解决方案


您正在where文档上使用,因为where它是仅适用于集合的查询功能。请注意,快照将返回一组快照,因为它是一个查询,而不是单个文档。

试试这个:

const snapshot = db.collection('items').doc(interaction.guildId).collection('items').where("ownerId", "==", ownerId).get();

推荐阅读