[英]MongoDB map a JSON response into a string
var db = mongoose.connection;
const FoundWarning = db.collection('warning').find({UserID: Warned.user.id, guildID: message.guild.id}).toArray(function(err, results) {
console.log(results);
})
I was trying to find more than one collection using MongoDB, I don't quite know how to map it and turn it into a String as there are multiple responses and not just one JSON document to use so I would have to map through them,我试图使用 MongoDB 找到多个集合,我不太知道如何映射它并将其转换为字符串,因为有多个响应,而不仅仅是一个 JSON 文档要使用,所以我必须通过它们进行映射,
Can someone explain how I can turn this Data to chunks that I can use like a string, an object or an array?有人可以解释一下如何将这些数据转换为可以像字符串、对象或数组一样使用的块吗?
I suppose I have to parse it and map it first, correct me if I'm wrong But I don't think I know how to do that我想我必须先解析它并映射它,如果我错了,请纠正我但我认为我不知道该怎么做
Here is the photo of the current result that I have这是我当前结果的照片
I wanted to (for example) get the "Reason" part of it in a string alone and send each "Reason" along side with the "UserName" and send it as a message on discord using discord.js so like loop through them for example我想(例如)将它的“原因”部分单独放在一个字符串中,并将每个“原因”与“用户名”一起发送,并使用 discord.js 将其作为不和谐消息发送,例如循环遍历它们例子
Edit: i forgot to mention that "Warned" is编辑:我忘了提到“警告”是
let Warned = message.mentions.members.first();
Sorry if I'm not good at explaining, I would appreciate edits if you understand what's the issue that I'm facing抱歉,如果我不擅长解释,如果您了解我面临的问题,我将不胜感激
You can simply access the Reason
property of an object.您可以简单地访问对象的
Reason
属性。 What you are facing is an array of objects and each object inside the array contains a Reason
property.您面对的是一个对象数组,数组中的每个对象都包含一个
Reason
属性。 You can either access one object, for example, results[0]
and then access the Reason
property like this results[0].Reason
or you can store all the reasons inside an array like this:您可以访问一个对象,例如,
results[0]
然后像这样访问Reason
属性results[0].Reason
或者您可以将所有原因存储在一个数组中,如下所示:
const reasons = results.map(result => result.Reason);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.