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

私は "http:// localhost:4730 /"というURLに行くとjsonとページが見えますが、wordpressでデータを送信しないと、 – First

+0

のような問題は通常簡単には解決されません。少なくともあなたは1つのことを今解決しました。あなたのWordpressのセットアップ、プラグイン、そしてあなたが達成しようとしていることについての詳細を与える新しい質問をしてみてください。 –

+0

よろしくお願いします。 – First

関連する問題