【问题标题】:Express REST API not returning integer arrayExpress REST API 不返回整数数组
【发布时间】:2013-09-13 22:49:23
【问题描述】:

我是第一次使用 Express,但遇到了问题。我正在尝试修改 this tutorial 并出于我的目的,但我使用的是 Redis 而不是 MongoDB。

这是我的代码:

redisSource.js:

var redis = require ("node-redis");

RedisSource = function () {
        this.r_client = redis.createClient();

        this.getParents = function (circuit_id, cb) {
                this.r_client.smembers('circuit.parents_of:' + circuit_id, function(err, reply){
                        console.log("Reply: " + reply);
                        cb(err, reply);
                });
        }
}

exports.RedisSource = RedisSource;

fetch.js:

Fetch = function(app) {
        var RedisSource = require ('./redisSource').RedisSource;
        var redisSource = new RedisSource();

        app.get('/parents/:id', function(req, res) {
                redisSource.getParents(req.params.id, function (error, parents) {
                        console.log ("Response in Fetch main: " + parents);
                        res.send(parents);
                });
        });

        app.get('/test', function (req, res) {
                res.send('Hello, world!');
        });
};

exports.Fetch = Fetch;

app.js:

/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);

var Fetch = require('./fetch').Fetch;
var FetchService = new Fetch(app);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

当我运行应用程序时,我得到以下信息:

GET http://ironsides.zayo.com:3000/test
> Hello, world!

这是我所期望的。但是当我尝试另一个电话时:

GET http://ironsides.zayo.com:3000/parents/12115
> [ [ 49, 53, 50, 55, 51 ], [ 49, 53, 50, 56, 56 ], [ 49, 53, 51, 48, 56 ], [ 49, 53, 51, 48, 57 ], [ 49, 53, 51, 49, 48 ], [ 49, 53, 51, 49, 49 ], [ 49, 53, 51, 50, 56 ], [ 49, 53, 51, 50, 57 ], [ 49, 53, 51, 51, 48 ], [ 49, 53, 51, 52, 49 ], [ 51, 51, 50, 54, 51 ] ]

在控制台上,我得到了这个:

GET /parents/12115 200 74ms - 530b
Reply: 15273,15288,15308,15309,15310,15311,15328,15329,15330,15341,33263
Response in Fetch main: 15273,15288,15308,15309,15310,15311,15328,15329,15330,15341,33263

我期待在控制台上看到的整数数组。相反,我得到了这些整数的 ascii 字符代码数组的数组。我真的很困惑。我确定我错过了一些简单的东西。

【问题讨论】:

  • 您是否尝试将结果作为 JSON 发送?
  • @max - 像 JSON.stringify?是的,结果相同。
  • 我的意思是在fetch.js 中以res.send({parents: parents}); 发送。
  • @max - 结果相同,但有父母:在它之前和{}在它周围。
  • 我认为是这个原因:stackoverflow.com/questions/8329252/…

标签: node.js rest express redis


【解决方案1】:

怎么样:

res.send(parents.toString());

【讨论】:

    【解决方案2】:

    在 max 的 cmets 和 this thread 之间,我想通了。 “node-redis”模块返回缓冲区,而不是字符串。如果我重写语句:

    console.log ("Response in Fetch main: " + parents);
    

    作为

    console.log (parents);
    

    然后它作为缓冲区数组返回。所以我像这样重写了 redisSource.getParents():

        this.getParents = function (circuit_id, cb) {
                this.r_client.smembers('circuit.parents_of:' + circuit_id, function(err, reply){
                        console.log("Reply: " + reply);
                        var string_reply = reply.map(function(item) { return item.toString('utf8'); });
                        cb(err, string_reply);
                });
        }
    

    如您所见,这将返回一个字符串数组而不是缓冲区数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-26
      • 2018-07-03
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 2018-03-15
      • 1970-01-01
      • 2013-06-03
      相关资源
      最近更新 更多