/ / Problema JSON prop / campo con MongoDB - javascript, json, node.js, mongodb, express

Problema JSON prop / campo con MongoDB - javascript, json, node.js, mongodb, express

Tengo una respuesta de "solicitud" con el siguiente código:

collection.distinct("id", (err, docs) => {
docs.forEach(id => {
let url = "url.com/id=" + id
request(url, (error, response, body) => {
var resp = JSON.parse(response.body.replace("._", "_"));
collection2.insert(resp);
});
});
});

response.body devuelve un JSON en cadena con algunos campos y propiedades que contienen un punto, por ejemplo:

MISC._EXTERIOR_FEATURES": {
"id": 29,
"name": "MISC._EXTERIOR_FEATURES",
"attributes": {
"ROOF_RACK": {
"id": 0,
"name": "ROOF_RACK",
"value": "roof rack"
}
}

Código explicado: para cada identificación distinta en la colección, solicite la url para identificación. Luego, usando response.body (string), usé .replace () para "limpiar" el JSON stringified, luego lo analizo y lo inserto en collection2.

Errores:

(node:24552) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 33): Error: key MISC._EXTERIOR_FEATURES must not contain "."
(node:24552) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 34): Error: key MISC._INTERIOR_FEATURES must not contain "."
(node:24552) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 35): Error: key MISC._EXTERIOR_FEATURES must not contain "."
(node:24552) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 36): Error: key MISC._INTERIOR_FEATURES must not contain "."
(node:24552) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 37): Error: key MISC._INTERIOR_FEATURES must not contain "."
(node:24552) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 38): Error: key MISC._INTERIOR_FEATURES must not contain "."
(node:24552) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 39): Error: key MISC._EXTERIOR_FEATURES must not contain "."

Respuestas

2 para la respuesta № 1

No estoy seguro de por qué la clave no debe contener ".", Pero su "reemplazo" reemplaza solo la primera vez. Prueba regex.

.replace(/._/g, "_")