【发布时间】:2016-07-23 01:17:18
【问题描述】:
如果我向我的 node.js(EXPRESS) API curl http://127.0.0.1:3000/api/events/user/id/1 发出 curl 请求,我会得到以下结果:
[{"id":"1","name":"casamiento 1","description":"el casamiento del tio claudio","mode_id":1,"initial_date":"2016-05-28T22:14:57.000Z","end_date":"2016-05-28T22:14:58.000Z","state_id":1,"user_id":"1","location":"0101000020E61000000000000000805BC00000000000003E40"},{"id":"2","name":"casamiento 2","description":"el casamiento del tio claudio 2","mode_id":1,"initial_date":"2016-05-28T22:14:57.000Z","end_date":"2016-05-28T22:14:58.000Z","state_id":1,"user_id":"1","location":"0101000020E61000000000000000405BC00000000000003D40"},{"id":"3","name":"fiesta del sandwich de miga","description":"Nos juntamos a comer sandwiches de miga hasta reventar","mode_id":1,"initial_date":"2016-05-28T22:15:58.000Z","end_date":"2016-05-28T22:15:58.000Z","state_id":1,"user_id":"1","location":"0101000020E610000000000000000000000000000000804840"}]
我需要用大括号将输出括起来,例如:
{{"id":"1","name":"casamiento 1","description":"el casamiento del tio claudio","mode_id":1,"initial_date":"2016-05-28T22:14:57.000Z","end_date":"2016-05-28T22:14:58.000Z","state_id":1,"user_id":"1","location":"0101000020E61000000000000000805BC00000000000003E40"},{"id":"2","name":"casamiento 2","description":"el casamiento del tio claudio 2","mode_id":1,"initial_date":"2016-05-28T22:14:57.000Z","end_date":"2016-05-28T22:14:58.000Z","state_id":1,"user_id":"1","location":"0101000020E61000000000000000405BC00000000000003D40"},{"id":"3","name":"fiesta del sandwich de miga","description":"Nos juntamos a comer sandwiches de miga hasta reventar","mode_id":1,"initial_date":"2016-05-28T22:15:58.000Z","end_date":"2016-05-28T22:15:58.000Z","state_id":1,"user_id":"1","location":"0101000020E610000000000000000000000000000000804840"}}
我的模型“事件”文件是这样的,它使查询到 knex,然后返回结果:
var express = require('express');
var router = express.Router();
var Promise = require("bluebird");
var connectionString = 'postgres://postgres:postgres@localhost:5432/flock';
var knex = require('knex')({
client: 'pg',
connection: connectionString,
searchPath: 'knex,public'
});
//Get all events from a particular user
exports.getUserEvents=function(id_user){
console.log("Retrieving data from id_user: "+id_user);
var promise1 = Promise.try(function () {
return knex('events')
.select('*')
.where('user_id', id_user);
});
var promise2=promise1.then(function (rows) { // this creates a new promise, and the promise created here is what gets returned to the caller
console.log('Returning '+rows.length+' rows from the user with id '+id_user);
return rows;
});
return promise2;
}
而我的路由文件,调用了模型文件函数getUserEvents,是这样的:
var express = require('express');
var router = express.Router();
var Event=require('../models/event');
//get all events from a user
router.get('/user/id/:id_user', function(req, res, next) {
var id_user= req.params.id_user;
var promise = Event.getUserEvents(id_user);
promise.then(function (result) {
console.log('Sending response');
return res.json(result); //<---this line builds the JSON response
});
promise.catch(function (err) {
return res.sendStatus(500);
});
});
module.exports = router
我的问题是,我如何发送由 {} 而不是由 [] 包围的 json 对象列表,就像现在返回一样?。非常感谢
编辑:这解决了我的问题,最终格式是 {"rows":[{row1},{row2},etc]}
exports.getUserEvents=function(id_user){
console.log("Retrieving data from id_user: "+id_user);
var promise1 = Promise.try(function () {
return knex('events')
.select('*')
.where('user_id', id_user);
});
var promise2=promise1.then(function (rows) { // this creates a new promise, and the promise created here is what gets returned to the caller
console.log('Returning '+rows.length+' events from the user with id '+id_user);
return {rows};//<----This solved the issue
});
return promise2; }
【问题讨论】:
-
为什么你需要那个精确的输出(那是无效的)?您能否也回答您要将该输出提供给什么?为什么数组 [{},{}] 或类似数组的对象 {1:{},2:{}} 不合适?
标签: json node.js express knex.js