我有此代码:
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
}
如果我只是return results
,它就可以完美工作。
但是为什么我不能返回recommendations
。它返回一个空白屏幕。
我的问题与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)
)
答案 0 :(得分:1)
我知道了!
我只需要这样做:
.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'")
})
即使用承诺