2016-10-04 4 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.json이 이미 처리하므로 res.type()을 제거하십시오.

+0

"http : // localhost : 4730 /"url로 이동하면 json을보고 페이지에서 보았습니다. 그러나 wordpress에서 데이터를 보내지 않으면 아무것도 표시되지 않습니다. – First

+0

이런 문제는 대개 쉽게 해결되지 않습니다. 적어도 지금 한 가지를 풀었습니다. Wordpress 설정, 플러그인 및 달성하려는 내용에 대해 더 자세히 설명하는 새로운 질문을 시도해보십시오. –

+0

오, 고마워요! – First

관련 문제