node.js - 'collection#find: pass a function instead, Tags.create is not a function' 的原因

标签 node.js sqlite sequelize.js discord.js

我正在设置一个 Discord 机器人,我正在制作一个警告命令,其中所有条目都将记录在 Sequelize 表中。目前,我也在使用主 index.js ,并且有一个包含所有命令的文件夹。 index.js 在机器人启动时调用每个命令。我正在使用 https://discordjs.guide/ 寻求帮助和指导。我使用了他们的 Seqeulize 数据库帮助,所以我正在“添加一个标签”,并且我完全按照他们所说的放置,除了我自己的列名。
我没有尝试过任何东西,因为我不知道为什么会发生这种情况。我刚开始学 JS 和自学,所以我知道的不多。

const Tags = sequelize.define('moderation', {
  username: {
    type: Sequelize.STRING,
    unique: false,
  },
  case_num: {
    type: Sequelize.INTEGER,
    unique: true,
  },
  type: Sequelize.STRING,
  reason: Sequelize.STRING,
  length: Sequelize.STRING,
  by: { 
    type: Sequelize.STRING,
    unique: false,
  },
  date: {
    type: Sequelize.DATE,
  }
});
const Discord = require("discord.js");
const {parseUser} = require('../util/parseUser.js');
const {caseNumber} = require('../util/caseNumber.js');
const Sequelize = require("sequelize");
const Tags = require ('../modTable.js');

module.exports.run = async (bot, message, args) => {
  const sequelize = new Sequelize('database', 'user', 'password', {
    host: 'localhost',
    dialect: 'sqlite',
    logging: false,
    operatorsAliases: false,
    // SQLite only
    storage: 'database.sqlite',
  });
    if(!message.member.hasPermission(['KICK_MEMBERS'])) 
      return message.reply("Sorry, you don't have permissions to use this!");


    const user = message.mentions.users.first();
    if (user === message.author) {
        return message.channel.send('You cannot do that to yourself, why did you try?');
    }
    const modlogs = bot.channels.find('name', 'mod-logs');
    const caseNum = await caseNumber(bot, modlogs);
    if (!modlogs) return message.reply('I cannot find a mod-log channel');
    if (message.mentions.users.size < 1) return message.reply('You must mention someone to warn them.').catch(console.error);
    const reason = args.splice(1, args.length).join(' ') || `no reason provided`;
    let date = new Date();
    const type_warn = "warn";

    try {
      const tag = await Tags.create({
        username: message.mentions.users.first(),
        case_num: caseNum,
        type: type_warn,
        reason: reason,
        length: null,
        by: message.author.username,
        date: date,
      });
      console.log(e);
    }
    catch (e) {
      console.log(e);
      return message.reply('Something went wrong with adding this warn to the table. Please contact Charlee or VorTex eXDee!');
    }
我希望事情被记录下来并且终端上不会出现错误,但它说:

Deprecationwarning: collection#find: pass a function instead

TypeError: Tags.create is not a function.

最佳答案

Collection#find: pass a function instead



Collection.find() 可以采用函数而不是属性和值。不推荐使用后者。

解决方案:

Collection.find(element => element.property === value)

(您也可以通过其他方式比较属性,并且可以同时比较多个属性。)

Tags.create() is not a function



当您需要 modTable.js 时,您将 Tags 声明为文件的 module.exports 属性。您可以为 module.exports 赋值和创建属性,以便您可以在其他地方访问您的变量。

解决方案:

从文件中导出变量。

module.exports = Tags;
// You can also use: module.exports.Tags = Tags;

关于node.js - 'collection#find: pass a function instead, Tags.create is not a function' 的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56520329/

相关文章:

node.js - forEach 中的 Node 惰性和异步函数

android - 数据库全局实例

sqlite - SQLite的总和

objective-c - 如何在保留斯堪的纳维亚字母的同时一直为 SQLite 正确编码 NSString

node.js - 意外的代币 * Koa.js

json - Node.js body-parser 不会解析 json 中带有方括号的输入名称

javascript - 使用 NodeJS 遍历 mongo 数据流时内存泄漏

model - 如何使用 or (Op.or/$or) 运算符指定该阶段应等于 null OR Junior 或 Senior 进行 Sequelize ?

javascript - Sequelize 包含范围为 1 :m constraint = false 的对象

mysql - 如何在 sequelize 中将查询结果作为数组获取?