I'm trying to manipulate the data that is sent back to the client via Deepstream with the new dataTransforms
API, however, I always get Uncaught SyntaxError: Unexpected end of input
in the console. It might be that it takes too long to make the database lookup for Deepstream but I'm not quite sure.
My relevant code is:
DSServer.set('dataTransforms', [
{
topic: DSServer.constants.TOPIC.RECORD,
action: DSServer.constants.ACTIONS.READ,
transform: transformRecord
}
]);
var transformRecord = function (data, metadata) {
if (metadata.recordName.split('/')[0] === 'team') {
var new_member_info = [];
var i = 0;
_.forEach(data.members, function (members) {
r.table('user').get(members.user_id).run()
.then(function (doc) {
if (doc !== null) {
new_member_info.push({
user_id: members.user_id,
display_name: doc._d.display_name,
username: doc._d.username
});
i += 1;
if (i === data.members.length) {
data.members = new_member_info;
return data;
}
}
})
.error(function (err) {
console.error(err);
});
});
} else {
return data;
}
};
Whenever there is READ from a RECORD it will check if it's a read from the team record. If it is a read from the team-record it will fetch all members that are a part of that team and add it to members: {}.
When it has iterated over all members and added the information about them it will return the new data.
So, any idea what might be wrong?
Am I understanding the dataTransforms
wrong?