express res.send({}) 无法返回Set和Map 对象
发布于 6 年前 作者 Borkes 3618 次浏览 来自 问答

express@4.15.3

router.get('/set-object', async (req, res) => {
    let a = new Set([1, 2, 3]);
    let b = new Map();
    b.set('hello', 'world')
    res.send({a: a, b: b}); //打印出来的a,b是有值的
})

请求返回:

{
    "a": {},
	"b": {}
}

res.send 或者res.json是无法返回Set对象,Map对象吗?

2 回复

看了一下源码express 下的 response.js ,是被下面语句给处理了:

var body = stringify(val, replacer, spaces, escape)

在你这种情况下,使用res.send会绕到res.json,然后再绕到 res.send。

@ghostcode 我大概了解了,Set Map不是json类型。 stackoverflow

回到顶部