0

I have this code:

    var results_ = ger.recommendations_for_person('movies', query.name, {actions: {likes: 1}});
  var recommendations = results_.recommendations;

  return recommendations; 

ger.recommendations_for_person('movies', query.name, {actions: {likes: 1}}); is meant to return something as such.

 {
      "recommendations": [
        {
          "thing": "spiderman",
          "weight": 1.6666666666666667,
          "last_actioned_at": "2019-05-17T23:06:54+01:00",
          "last_expires_at": "2020-06-06T01:00:00+01:00",
          "people": [
            "bob",
            "alice"
          ]
        },
        {
          "thing": "xmen",
          "weight": 1.6666666666666667,
          "last_actioned_at": "2019-05-17T23:06:54+01:00",
          "last_expires_at": "2020-06-06T01:00:00+01:00",
          "people": [
            "alice",
            "bob"
          ]


        },
        {
          "thing": "barbie",
          "weight": 1,
          "last_actioned_at": "2019-05-17T23:06:54+01:00",
          "last_expires_at": "2020-06-06T01:00:00+01:00",
          "people": [
            "alice"
          ]
        },
        {
          "thing": "avengers",
          "weight": 0.6666666666666667,
          "last_actioned_at": "2019-05-17T23:06:54+01:00",
          "last_expires_at": "2020-06-06T01:00:00+01:00",
          "people": [
            "bob"
          ]
        }
      ],
      "neighbourhood": {
        "bob": 0.6666666666666667,
        "alice": 1
      },
      "confidence": 0.002462038997842016
    }

And it works perfectly if I just return results. But why can't I return recommendations.It returns a blank screen.

My question is different from How do I return the response from an asynchronous call? because for one thing ,I am using nodejs not ajax.It is meant to be synchronous. This is the full code for `

recc_.js:

var http = require('http'),
    url = require('url');
// require the ger objects
http.createServer(function (request, response) {
  var query = url.parse(request.url,true).query;

var g = require('../ger')

// Create an Event Store Manager (ESM) that stores events and provides functions to query them
var esm = new g.MemESM()

// Initialize GER with the esm
var ger = new g.GER(esm);

ger.initialize_namespace('movies')
.then( function() {
  return ger.events([


 ////RECCOMMENDATION LISTS



    { 
      namespace: 'movies', 
      person: 'bob', 
      action: 'likes', 
      thing: 'xmen',
      expires_at: '2020-06-06' 
    },
    { 
      namespace: 'movies', 
      person: 'bob', 
      action: 'likes', 
      thing: 'avengers',
      expires_at: '2020-06-06' 
    },

    { 
      namespace: 'movies', 
      person: 'bob', 
      action: 'likes', 
      thing: 'spiderman',
      expires_at: '2020-06-06' 
    },

    { 
      namespace: 'movies', 
      person: 'alice', 
      action: 'likes', 
      thing: 'xmen',
      expires_at: '2020-06-06' 
    },
    { 
      namespace: 'movies', 
      person: 'alice', 
      action: 'likes', 
      thing: 'spiderman',
      expires_at: '2020-06-06' 
    },
    { 
      namespace: 'movies', 
      person: 'alice', 
      action: 'likes', 
      thing: 'barbie',
      expires_at: '2020-06-06' 
    },

 ////RECCOMMENDATION LISTS





  ])
})
.then( function() {
  // What things might alice like?



  var results_ = ger.recommendations_for_person('movies', query.name, {actions: {likes: 1}});
  var recommendations = results_.recommendations;
  //var results = results_[reccomendations.map(({ thing }) => thing)];

  return recommendations;


})
.then( function(recommendations) {
  response.end(JSON.stringify(recommendations,null,2))
  response.end("\nRecommendations For 'alice'")
})

}).listen(8080);


console.log(' server running ok http://127.0.0.1:8080/');

The implementation for var results_ = ger.recommendations_for_person('movies', query.name, {actions: {likes: 1}}) is :

recommendations_for_person: (namespace, person, configuration = {}) ->
    configuration = @default_configuration(configuration)
    actions = configuration.actions

    #first a check or two
    @find_events(namespace, actions: Object.keys(actions), person: person, current_datetime: configuration.current_datetime, size: 100)
    .then( (events) =>

      return {recommendations: [], confidence: 0} if events.length < configuration.minimum_history_required

      return @generate_recommendations_for_person(namespace, person, actions, events.length, configuration)
    )
Turbo
  • 124
  • 11

1 Answers1

1

I GOT IT!!!

I just had to do:

.then( function() {
  // What things might alice like?



   return results_ = ger.recommendations_for_person('movies', query.name, {actions: {likes: 1}});

})
.then(function(results_) {
  var recommendations = results_.recommendations;
  //var results = results_[reccomendations.m ap(({ thing }) => thing)];

  return recommendations;

  })
.then( function(recommendations) {
  response.end(JSON.stringify(recommendations,null,2))
  response.end("\nRecommendations For 'alice'")
})

ie using Promises

Turbo
  • 124
  • 11