我正在使用express和Discord.js。
website.js
const express = require("express");
const app = express();
//...
ap.get("/testpage/:guildID", checkAuth, (req, res) => {
const guild = client.guilds.get(req.params.guildID); //get the guild object which holds further object maps
//console.log(guild) output is expected with this console log
//I made the guildRoles since I thought maybe I had to send the roles map instead of the guild object, but it does not work either
const guildRoles = guild.roles;
//console.log(guildRoles) has expected output
renderTemplate(res, req, "configure.ejs", {guild, guildRoles});
};
configure.ejs
//...
<script>
//...
function testFunction() {
let guildRolesVar = <%- guildRoles %>;
console.log(guildRolesVar);
//this outputs an empty object, {}, when it should return {...} ... with data in it
//same with <%- guild %>, however, it does return data, however, object maps are empty (same as above), but things that are not maps
};
</script>
我希望对象映射数据能够通过,但是不会通过,我也不知道为什么。
guild
结构是:
Guild {
roles:
Collection [Map] {
'1111111111111110' => Role {
guild: [Circular],
id: '1111111111111110',
name: '@testRole' },
'1837219387129378' => Role {
guild: [Circular],
id: '1837219387129378',
name: '@anotherRole' } },
name: 'the guild name',
id: '12103981203980',
}
如您所见,行会对象还有其他对象映射。当我在app.get(...)
中使用console.log行会对象时,它会输出完整的数据而不会丢失任何内容。但是,当我通过对象时,角色对象为空({})。
为什么会发生这种情况,如何获取完整数据?
答案 0 :(得分:0)
这很可能发生,因为express将对它们进行JSON stringify(),并且因为discord.js对象具有循环引用,所以它们将为{}
。
一种可能的解决方案是使用d.js master / v12,从而允许在其对象上使用JSON.stringify()。