反斜杠在将 id 写入 mongoDB 时正在打印



我通过以下方式将idangular js传递到nodejs服务器

formData.append("proId", JSON.stringify($scope.pid));


我也能够在服务器端检索id,但是当我将其发布到 MongoDB 时,它会向 id 添加back-shlahes
例如:
""5a0fc8ad4a9a9c2cd81a8853"",它不包含和额外的"逗号。

为什么会这样?

这就是

JSON.stringify对字符串的作用:

→ node -pe 'JSON.stringify("5a0fc8ad4a9a9c2cd81a8853")'
"5a0fc8ad4a9a9c2cd81a8853"

您可能需要在服务器端JSON.parse它。

→ node -pe 'JSON.parse(""5a0fc8ad4a9a9c2cd81a8853"")'
5a0fc8ad4a9a9c2cd81a8853

或者不要首先对变量进行 JSON 编码。

最新更新