I'm stuck with the following problem.
My backend (Java sevlet) returns from a database all the fields from a single record/document from a MongoDB. This JSON string is sent to the frontend where some magic is done.
This is done with the following code:
public String getDocumentJSON(int id) {
DBCollection collection = database.getCollection("People");
BasicDBObject query = new BasicDBObject();
query.put("id", id);
DBCursor cur = collection.find(query);
DBObject one = cur.next();
return JSON.serialize(one);
}
The frontend requires a field: photos[]
New records/documents are created with this field. (it's an array with photo filenames). The older records/documents do not have this field, which causes the frontend to break.
To prevent this I would want to add the array: photos[]
to the JSON before sending it to the frontend. But I'm not to sure how I add this array to the JSON.
Preferably my code would check to see if the JSON or DBObject has the field photos[]
if it does, then nothing is done, if it doesn't contain this field. Then it is added.
The frontend just needed the photos[]
even if it's empty that's no problem.
I would rather fix this in the backend that in the frontend.