【发布时间】:2021-06-03 17:21:01
【问题描述】:
我正在使用 MongoDB 和 Mongoose 编写一个基本的 Discord.js 经济系统,而且我是初学者,所以我遇到了一个问题。我正在尝试添加一个代表工作的猫鼬模式。这是一个数字,0 代表没有工作,1 代表医生,2 代表警察,3 代表厨师。这是我的代码。
ProfileSchema.js(我在其中创建架构)
const mongoose = require("mongoose");
const profileSchema = new mongoose.Schema({
userID: { type: String, require: true, unique: true},
serverID: { type: String, require: true},
coins: { type: Number, default: 500},
bank: { type: Number},
job: { type: Number, default: 0}
})
const model = mongoose.model('ProfileModels', profileSchema);
module.exports = model;
这是我的 message.js 文件的一部分,它会在成员发送消息后立即为他们创建个人资料:
let profileData;
try{
profileData = await profileModel.findOne({ userID: message.author.id});
if(!profileData){
let profile = await profileModel.create({
userID: message.author.id,
serverID: message.guild.id,
coins: 500,
bank: 0,
job: 0
});
profile.save()
}
}catch(err){
console.log(err)
}
好的,我们差不多完成了。您可以在此处分配作业。
const profileModel = require("../models/profileSchema");
module.exports = {
name: 'getjob',
description: 'get a job',
async execute(client, message, args, Discord, profileData) {
if (!args.length) {
return message.channel.send(`You need to select a job!`);
}
else if (args[0] === 'doctor') {
return message.channel.send('You have selected the job: Doctor');
const job = 1
}
else if (args[0] === 'policeman') {
return message.channel.send('You have selected the job: Policeman);
const job = 2
}
else if (args[0] === 'chef') {
return message.channel.send('You have selected the job: Chef');
const job = 3
}
const response = await profileModel.findOneAndUpdate(
{
userID: message.author.id
},
{
$inc: {
job: job,
},
}
);
}
}
最后一段代码,我保证。这就是我认为出错的地方。我创建了一个测试命令来查看是否实际分配了作业,所以它基本上只是告诉你你有什么作业。
const profileModel = require("../models/profileSchema");
module.exports = {
name: "jobtest",
description: "jobtest",
execute(client, message, args, Discord, profileData){
if (profileData = 0) return message.channel.send(`You are unemployed!`)
if (profileData = 1) return message.channel.send(`You are a doctor.`)
if (profileData = 2) return message.channel.send(`You are an policeman.`)
if (profileData = 3) return message.channel.send(`You are a chef.`)
}
}
抱歉所有这些代码。基本上,问题是当我运行 ?jobtest 时,无论我应该从事什么工作,它总是说“你是一名医生”。有人能告诉我哪里出错了吗?谢谢!
【问题讨论】:
-
在你的最后一段代码中,你使用了一个
=,它应该是==或===作为条件
标签: javascript mongodb discord.js