[英]Item doesn't want to be pushed into an array
我正在开发一个 discord 机器人,它允许您挖掘资源,但我无法让代码工作。 它正在记录实际项目,但它只是不将项目添加到数组中,这就是为什么我不能将它 map 转换为字符串。
代码:
module.exports.addItem = async (client, user, item, amount, interaction) => {
if(!client instanceof Client) throw new Error("Client is not an instance of Discord.js Client");
if(!user instanceof User) throw new Error("User is not an instance of Discord.js User");
if(!item) throw new Error("Item is not defined");
if(!amount) throw new Error("Amount is not defined");
if(typeof amount !== "number") throw new Error("Amount is not a number");
if(!interaction) throw new Error("Interaction is not defined");
if(!interaction instanceof Interaction) throw new Error("Interaction is not an instance of Discord.js Interaction");
const userFound = await client.database.user.findOne({ userID: user.id });
if(!userFound) return interaction.followUp({ content: `You haven't started your journey yet!` });
const itemFound = userFound.inventory.find(i => i.name === item.name);
if(!itemFound) {
await client.database.user.findOneAndUpdate(
{
userID: user.id,
}, {
userID: user.id,
$push: {
inventory: {
name: item.name,
amount,
category: item.category
}
}
} , {
upsert: true,
});
} else {
await client.database.user.findOneAndUpdate(
{
userID: user.id,
}, {
userID: user.id,
$pull: {
inventory: {
name: item.name
}
}
} , {
upsert: true,
});
await client.database.user.findOneAndUpdate(
{
userID: user.id,
}, {
userID: user.id,
$push: {
inventory: {
name: item.name,
amount: item.amount + amount,
category: item.category
}
}
} , {
upsert: true,
});
}
return { name: item.name, amount, category: item.category };
}
module.exports.dropItem = async (client, user, category, interaction) => {
if(!client instanceof Client) throw new Error("Client is not an instance of Discord.js Client");
if(!user instanceof User) throw new Error("User is not an instance of Discord.js User");
if(!category) throw new Error("Category is not defined");
if(typeof category !== "string") throw new Error("Category is not a string");
let droppedItems = [];
for (const item of client.items) {
for(let i = 0; i < client.items.length; i++) {
if(!client.items[i].category === category) return;
}
const dropAmount = Math.floor((Math.random() * 3) + 1);
const dropChance = Math.floor(Math.random() * 100);
if(dropChance > item.dropChance) return;
const itemDropped = await this.addItem(client, user, item, dropAmount, interaction)
console.log(itemDropped); // This logs the item correctly
droppedItems.push(itemDropped);
}
return droppedItems; // This always returns "undefined"
}
预期结果:一个包含对象的数组,每个 object 包含名称、数量和类别值;
实际结果:控制台记录数组返回“未定义”
我已经尝试等待推送,使数组成为常量和更多类似的小东西。 难道是因为数组不在循环中?
问题可能是您的 dropItem function 中有几个 return 语句返回undefined
。
例如if(dropChance > item.dropChance) return;
一个简单的 function 来说明这一点是:
const doSomething = () => {
const arr = [1, 2, 3]
for (const num of arr) {
if (num !== 10) return
}
return 'done'
}
您是说您希望上面的 function 返回“完成”,但它实际上会返回undefined
。
我想也许您想使用break
而不是return
,但不确定您期望的行为到底是什么。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break
由于您正在等待 for 循环中的调用,因此您还需要等待整个循环。 您可以使用await Promise.all(<<ARRAY OF PROMISES>>)
执行此操作。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
所以你会做类似的事情
let droppedItems = [];
await Promise.all(client.items.map(async (item) => {
for(let i = 0; i < client.items.length; i++) {
if(!client.items[i].category === category) return;
}
const dropAmount = Math.floor((Math.random() * 3) + 1);
const dropChance = Math.floor(Math.random() * 100);
if(dropChance > item.dropChance) return;
const itemDropped = await this.addItem(client, user, item, dropAmount, interaction)
console.log(itemDropped); // This logs the item correctly
droppedItems.push(itemDropped);
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.