【发布时间】:2021-01-12 08:19:34
【问题描述】:
我正在尝试使用我的 discord.js 机器人和猫鼬创建一个货币系统。这是一个示例 MongoDB 文档格式:
{
guild: "2095843098435435435",
wallets: [
{
id: "2323232335354",
amount: 10,
},
{
id: "24344343234454",
amount: "i want to update this",
},
],
};
据我所知,Array.prototype.push() 函数用于在数组中创建一个新对象。
但是如何更新数组中的现有对象呢?
我的代码:
const find = await Account.findOne({
guild: message.guild.id,
});
if (!find) return message.channel.send("...");
const memberRole = message.guild.members.cache
.find((x) => x.id === message.author.id)
.roles.cache.find(
(x) =>
x.name.toLowerCase() === "tournament manager" ||
x.name.toLowerCase() === "events staff" ||
x.name.toLowerCase() === "wallet manager"
);
if (message.member.hasPermission("ADMINISTRATOR") || memberRole) {
if (!args[2]) return message.reply("...");
const mention = message.mentions.users.first();
if (!mention) return message.reply("...");
const account = find.wallets.find((x) => x.id === mention.id);
if (!account) return message.reply("...");
if (!args[3]) return message.reply("...");
if (isNaN(args[3])) return message.channel.send("...");
const update = account.update({
amount: (account.amount += args[3]),
});
await find.save();
}
【问题讨论】:
标签: node.js mongoose discord.js