2016-10-04 56 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