I have Nodejs Application and I want to send request and response to elastic, I have an issue with mapping, so basically my code looks like this:
static sendLogToElastic = (req, res, next) => {
const self = this;
const originalSend = res.send;
res.send = async function (responseBody) {
if (typeof responseBody === "object") {
console.log(responseBody);
if (req.body?.password) delete req.body.password;
if (req.body?.passwordConfirmation)
delete req.body.passwordConfirmation;
self.esClient
.index({
index: process.env.ELASTIC_REQUEST_RESPONSE_INDEX,
document: {
user_id: req.user?.id || responseBody?.data?.user?.id,
date: new Date(),
request: {
originalUrl: req.originalUrl,
params: req.params,
query: req.query,
body: req.body,
clientIp: req.headers["x-client-ip"],
deviceId: req.headers["x-device-id"],
},
response: {
body: responseBody,
},
},
})
.then((value) => {
logger.info(JSON.stringify(value));
})
.catch((err) => {
logger.error(
`request: ${req.originalUrl}, sendLogToElastic error: ${err}`
);
});
}
originalSend.apply(res, arguments);
};
next();
};
This sends all the requests and responses to the elastic but in the response object, the data field sometimes is an object or a number, or just a string. But Elastic doesn't accept when it's a number or string. I got the following error:
sendLogToElastic error: ResponseError: document_parsing_exception
Root causes:
document_parsing_exception: [1:213] object mapping for [response.body.data] tried to parse field [data] as object, but found a concrete value