簡體   English   中英

Node.js、wordpress、redis

[英]Node.js, wordpress,redis

我需要從節點服務器中的 Redis 獲取數據並將其發送到 Wordpress 布局。 我嘗試以下,但data似乎並沒有被發送。

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);

看來你的范圍不正確。 this.data指的是function (req, res) {}而不是您的全局范圍。

嘗試執行res.json(data)並刪除res.type() ,因為res.json已經為您處理好了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM