gpt4 book ai didi

javascript - 将对象写入文件时的 Node JS [对象映射]

转载 作者:行者123 更新时间:2023-11-30 11:22:04 25 4
gpt4 key购买 nike

我在 stackoverflow 和其他教程网站上阅读了很多关于将对象/JSON 写入文件的帖子,但没有一个解决方案适合我。

1)代码:

let messages = await fetchMessages()
console.log(messages) // Prints an object
fs.writeFileSync('./msgdata.json', messages , 'utf-8');

2)也试过,

fs.writeFileSync('./msgdata.json', JSON.stringify(messages) , 'utf-8'); 

3) 也试过,

fs.writeFile(),但得到与上面相同的输出。

msgdata.json:(对于 1)

[object Map]

msgdata.json:(对于 2 和 3)

{}

谁能指出是什么原因造成的?

## console.log(messages) 的输出##

OUTPUT contains 10 more such objects having different id's:

channel:
TextChannel {
type: 'text',
id: '424825532274253312',
name: 'request',
position: 69,
parentID: '397363224286473987',
permissionOverwrites: [Object],
topic: null,
nsfw: false,
lastMessageID: '427143105410760704',
guild: [Object],
messages: [Object],
_typing: Map {} },
id: '427142596817846272',
type: 'DEFAULT',
content: '**Select your emoji:** __***Group 9:***__',
author:
ClientUser {
id: '407083773537350272',
username: 'Bot',
discriminator: '9256',
avatar: '9c374e719ba2ab4e69fd577005b635bf',
bot: true,
lastMessageID: null,
lastMessage: null,
verified: true,
email: null,
localPresence: {},
_typing: Map {},
friends: Collection {},
blocked: Collection {},
notes: Collection {},
premium: null,
mfaEnabled: false,
mobile: null,
settings: [Object],
guildSettings: Collection {} },
member:
GuildMember {
guild: [Object],
user: [Object],
_roles: [Array],
serverDeaf: false,
serverMute: false,
selfMute: undefined,
selfDeaf: undefined,
voiceSessionID: undefined,
voiceChannelID: undefined,
speaking: false,
nickname: null,
joinedTimestamp: 1517127343434,
lastMessageID: null,
lastMessage: null },
pinned: false,
tts: false,
nonce: undefined,
system: false,
embeds: [],
attachments: Collection {},
createdTimestamp: 1521909131007,
editedTimestamp: null,
reactions:
Collection {
'taillow:417281639777959940' => [Object],
'shroomish:417281639899463680' => [Object],
'sableye:417281640197521419' => [Object],
'ralts:417281642735075329' => [Object],
'sentret:417281644001624076' => [Object],
'shuppet:417281644291162132' => [Object],
'torchic:417281647210397706' => [Object],
'snubbull:417281647692480522' => [Object],
'sunkern:417281647763783681' => [Object],
'slowpoke:417281648653107200' => [Object],
'teddiursa:417281649537974273' => [Object],
'sneasel:417281649613471747' => [Object],
'snorunt:417281649819123712' => [Object],
'surskit:417281650163056640' => [Object],
'qwilfish:417281654629859348' => [Object],
'shelgon:417281654730522624' => [Object] },
mentions:
MessageMentions {
everyone: false,
users: Collection {},
roles: Collection {},
_content: '**Select your emoji:** __***Group
9:***__',
_client: [Object],
_guild: [Object],
_members: null,
_channels: null },
webhookID: null,
hit: null,
_edits: [] },

最佳答案

这是因为返回的对象是一个 map 对象 (MDN)。当您JSON.strigify 一个 Map 对象时,它总是返回 {}。有两种方法可以获取存储 map 的可读 JSON。第一个是遍历所有条目并创建一个 JSON(考虑键和值)。这应该很容易实现。

另一种方法是从 Map 创建一个数组,然后将其字符串化。

fs.writeFileSync('./msgdata.json', JSON.stringify([...myObject]) , 'utf-8'); 

关于javascript - 将对象写入文件时的 Node JS [对象映射],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49473229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com