node.js - Discord.js/Firestore .where() 不是函数
问题描述
我正在尝试将我的不和谐机器人与 Firestore 集成。每当我尝试运行查询时,我得到 .where 不是一个函数,我不明白为什么,因为其他一切似乎都有效。这是相关的代码。我已经在 Remove.js 的顶部尝试了 firebase 的要求,但这似乎没有任何作用。这是我认为它现在应该如何工作的想法。
- 我运行节点。然后它运行我的 index.js 文件。
- 在交互创建(即创建斜杠命令)时,它会检查命令文件,在这种情况下,它是删除命令
- 它调用 execute(interaction, db),其中interaction 是交互斜杠命令,db 是 index.js 中的 admin.Firestore() db 引用。我完全能够使用 get 命令(即在我尝试删除之前,第一块代码有效)
- 因为这是一个参考,我应该能够根据 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!`)
},
};
解决方案
您正在where
文档上使用,因为where
它是仅适用于集合的查询功能。请注意,快照将返回一组快照,因为它是一个查询,而不是单个文档。
试试这个:
const snapshot = db.collection('items').doc(interaction.guildId).collection('items').where("ownerId", "==", ownerId).get();
推荐阅读
- python - 我正在尝试创建一个猜谜游戏,但是当我运行我的代码时,什么也没有发生
- yacc - Yacc 冲突我无法解决
- android - Android 手机无法使用“android.se.omapi.Reader.isSecureElementPresent()”找到自定义 SIM 卡
- python - plotly:突出显示/识别 choropleth 中的某些国家/地区
- swagger - Swagger-Codegen .Net-3.5 - 如何删除生成代码中的默认参数
- javascript - 为什么 map() 只显示数组中的最后一个对象?
- python - 使用 Python ftplib 上传文件时出现“不允许操作”或“不是常规文件”
- ssis - 获取过去 7 天的数据 DB2
- python - 已指定零间距时如何在 matplotlib 中删除额外的填充
- java - ReactiveX 背压无法按预期工作