javascript - 变量返回空

标签 javascript arrays node.js object

我有这个代码:

    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}}); 意味着返回这样的东西。

 {
      "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
    }

如果我只是返回结果,它就会完美地工作。 但为什么我不能返回推荐。它返回一个空白屏幕。

我的问题与 How do I return the response from an asynchronous call? 不同因为一方面,我使用的是 nodejs 而不是 ajax。它是同步的。 这是`的完整代码

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/');

var results_ = ger.recommendations_for_person('movies', query.name, {actions: {likes: 1}}) 的实现是:

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)
    )

最佳答案

我明白了!!!

我只需要做:

.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'")
})

即使用 Promise

关于javascript - 变量返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56194913/

相关文章:

javascript - 附加元素未按预期工作

javascript - Chart.js 缩放开始于放大

c - 无法将 (int*) 转换为 int

arrays - 使用V8引擎的二进制数组

node.js - 如何更改进程列表中的命令行?

调用javascript函数的php代码返回函数未定义

javascript - 使用 Jquery 关闭 Iframe?

arrays - 行专业到列专业的转换公式?

node.js - 在 NestJs 项目中使用 i18n 库翻译类验证错误消息

javascript - 如何在 Node 中解析 POST 响应体