Reputation: 783
I am using Elasticsearch in order to store all the logging from my REST API with Winston. After testing it around and making some requests through my API, I notice that I get 'object mapping for [fields.meta.res.body.x] tried to parse field [x] as object, but found a concrete value'
multiple times. That happens because I don't always populate the fields, that can result in either a field with an ID or a field with an object with the populated data.
e.g. The response can either be:
{
"_id": "5c6be1ab42cb9db50832469e",
"name": "xyz",
"role": "5c3e003efb6fc0600be0d642"
}
or
{
"_id": "5c6be1ab42cb9db50832469e",
"name": "xyz",
"role": {
"_id": "5c3e003efb6fc0600be0d642",
"name": "admin"
}
}
Is there any way that I can make a field accept two different types of values? If not, what's the best way to work around that issue? Thank you!
Upvotes: 0
Views: 828
Reputation: 217314
You cannot have an object
field (i.e. role
) contains a concrete value (i.e. a string) and vice versa. You need to decide on one or the other. In your case, what would seem reasonable is to always specify role
as an object and leave role.name
to null if there's no value. If instead of this:
{
"_id": "5c6be1ab42cb9db50832469e",
"name": "xyz",
"role": "5c3e003efb6fc0600be0d642" <-- this is no good since role is an object
}
use this:
{
"_id": "5c6be1ab42cb9db50832469e",
"name": "xyz",
"role": {
"_id": "5c3e003efb6fc0600be0d642", <-- specify the id here
"name": null <-- use null here
}
}
And if you suffer from the opposite problem (i.e. trying to fit an object into a string), then you need to make sure that role
actually contains the value of role._id
, i.e. instead of this
{
"_id": "5c6be1ab42cb9db50832469e",
"name": "xyz",
"role": {
"_id": "5c3e003efb6fc0600be0d642",
"name": null
}
}
use this:
{
"_id": "5c6be1ab42cb9db50832469e",
"name": "xyz",
"role": "5c3e003efb6fc0600be0d642" <-- this should be role._id
}
Upvotes: 1