findByPrimary 不是函数
findByPrimary is not a function
TL;DR
当我使用 Sequelize 时,我收到一条错误消息,指出 findByPrimary 不是一个函数。
我一直在关注 this tutorial 如何使用 Sequelize 和 SQLite 3 为 Discord 机器人制作货币系统。但是,每当我在模型上使用 findByPrimary
时,我都会收到以下错误:
(node:9182) UnhandledPromiseRejectionWarning: TypeError: Users.findByPrimary is not a function
Users
定义在 models/Users.js
:
module.exports = (sequelize, DataTypes) => {
return sequelize.define('users', {
userId: {
type: DataTypes.STRING,
primaryKey: true
},
balance: {
type: DataTypes.INTEGER,
defaultValue: 0,
allowNull: false
}
}, {
timestamps: false
});
};
在dbObjects.js
中引用:
//modules
const Sequelize = require('sequelize');
//sequelize connection info
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'sqlite',
logging: false,
storage: 'database.sqlite'
});
//models
const Users = sequelize.import('models/Users');
//export
module.exports = {Users};
在 server.js
中导入并用作命令文件中 execute
中的参数之一:
const {Users} = require('./dbObjects');
//command is a file (in this case commands/inventory.js and commands/buy.js)
command.execute(message, Users);
在不起作用的命令中使用:
commands/inventory.js
module.exports = {
execute: async (message, Users) => {
const target = message.mentions.users.first() || message.author;
const user = await Users.findByPrimary(target.id);
}
};
commands/buy.js
module.exports = {
execute: async (message, Users) => {
const user = await Users.findByPrimary(message.author.id);
}
};
我曾尝试使用 findById
,但结果是相同的错误消息。我还尝试将以下代码添加到命令文件中的执行函数中:
const Sequelize = require('sequelize);
const SQLite = require('sqlite3');
我的代码与上述教程的唯一区别是我使用的是 command handler.
所有其他 Sequelize 功能,例如 findAll
一直在工作。
请使用 findByPk 而不是 findByPrimary
请参阅续集文档:http://docs.sequelizejs.com/manual/models-usage.html
Users.findByPk(id)
TL;DR
当我使用 Sequelize 时,我收到一条错误消息,指出 findByPrimary 不是一个函数。
我一直在关注 this tutorial 如何使用 Sequelize 和 SQLite 3 为 Discord 机器人制作货币系统。但是,每当我在模型上使用 findByPrimary
时,我都会收到以下错误:
(node:9182) UnhandledPromiseRejectionWarning: TypeError: Users.findByPrimary is not a function
Users
定义在 models/Users.js
:
module.exports = (sequelize, DataTypes) => {
return sequelize.define('users', {
userId: {
type: DataTypes.STRING,
primaryKey: true
},
balance: {
type: DataTypes.INTEGER,
defaultValue: 0,
allowNull: false
}
}, {
timestamps: false
});
};
在dbObjects.js
中引用:
//modules
const Sequelize = require('sequelize');
//sequelize connection info
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'sqlite',
logging: false,
storage: 'database.sqlite'
});
//models
const Users = sequelize.import('models/Users');
//export
module.exports = {Users};
在 server.js
中导入并用作命令文件中 execute
中的参数之一:
const {Users} = require('./dbObjects');
//command is a file (in this case commands/inventory.js and commands/buy.js)
command.execute(message, Users);
在不起作用的命令中使用:
commands/inventory.js
module.exports = {
execute: async (message, Users) => {
const target = message.mentions.users.first() || message.author;
const user = await Users.findByPrimary(target.id);
}
};
commands/buy.js
module.exports = {
execute: async (message, Users) => {
const user = await Users.findByPrimary(message.author.id);
}
};
我曾尝试使用 findById
,但结果是相同的错误消息。我还尝试将以下代码添加到命令文件中的执行函数中:
const Sequelize = require('sequelize);
const SQLite = require('sqlite3');
我的代码与上述教程的唯一区别是我使用的是 command handler.
所有其他 Sequelize 功能,例如 findAll
一直在工作。
请使用 findByPk 而不是 findByPrimary 请参阅续集文档:http://docs.sequelizejs.com/manual/models-usage.html
Users.findByPk(id)