2016-10-04 55 views
1

我需要从节点服务器中的Redis获取数据并将其发送到Wordpress布局。我尝试了以下,但data似乎并没有得到发送。Node.js,wordpress,redis

var express = require('express'); 
var app = express(); 
var redis = require('redis'); 
var client = redis.createClient(); //creates a new client 

client.on('connect', function() { 
    console.log('connected'); 
}); 

data = []; 

client.set(['first', 'Oleh']); 
client.set(['second', 'Ivan']); 
client.set(['thirt', 'Andriy']); 
client.set(['fourth', 'Olena']); 
client.set(['fifth', 'Kristy']); 
client.set(['sixth', 'Irina']); 

client.get('first', function (err, reply) { 
    console.log(reply); 
    data.push(reply); 
}); 

client.get('second', function (err, reply) { 
    console.log(reply); 
    data.push(reply); 
}); 

client.get('thirt', function (err, reply) { 
    console.log(reply); 
    data.push(reply); 
}); 

client.get('fourth', function (err, reply) { 
    console.log(reply); 
    data.push(reply); 
}); 

client.get('fifth', function (err, reply) { 
    console.log(reply); 
    data.push(reply); 
}); 

client.get('sixth', function (err, reply) { 
    console.log(reply); 
    data.push(reply) 
}); 

app.get('http://localhost/wordpress/', function (req, res) { 

    res.type('application/json'); // set content-type 
    res.send(this.data); // send text response 
    console.log(this.data); 
}); 

app.listen(process.env.PORT || 4730); 
+0

你能告诉我们你[现在](http://stackoverflow.com/help/mcve)吗? –

+0

编辑你的问题,并将其添加到那里。 –

回答

1

看来你的范围不正确。 this.data指的是function (req, res) {},而不是您的全球范围。

尝试做res.json(data)并删除res.type(),因为res.json已经为您照顾。

+0

如果我去url“http:// localhost:4730 /”我得到json并在页面中看到了,但是如果我在wordpress中发送数据什么都没有 – First

+0

像这样的问题通常不容易解决。至少你现在已经解决了一件事,试着问一个新的问题,它提供了关于你的Wordpress设置,插件和你想要实现的更多细节。 –

+0

okey,谢谢! – First