Search code examples
javascriptsocketsdeepstream.io

dataTransform in Deepstream.io gives 'Uncaught SyntaxError: Unexpected end of input' in the console


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?


Solution

  • For performance reasons, all data-transforms are expected to return a result synchronously.

    r.table('user').get(members.user_id).run()
            .then(...
    

    however runs asynchronously and returns its value to the function used in then, rather than the dataTransform function. It's also crucial to always return the data, whether transformations were applied or not.

    If I understand it correctly, your usecase is to load information for a number of users from a RethinkDb server. Might I recommend either using a list of recordnames for this usecase or build a data provider to interact with RethinkDb.