如果我对我的node.js(EXPRESS)API curl发出了curl请求,则会http://127.0.0.1:3000/api/events/user/id/1
得到以下结果:
[{"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; }
您的api请求返回一个对象数组
arrayOfObjects = [
{objkey: objvalue},
{objkey: objvalue}
]
您可以将数组嵌套在这样的对象中
newObj = {
nestedarray: result
}
或者您可以将对象作为单个值返回
newObj = {
1: result[0],
2: result[1],
3: result[2]
}
但是所有对象都需要一个键值对。
请注意,JavaScript中的Array实际上是一种特殊的对象类型,它仍然使用碰巧是整数的属性名称,但经过优化以允许使用特殊的方法。
因此,在您的情况下:
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');
// Option One
var newObj = {
nestedarray: result
}
// Option Two
var newObj = {}
response.forEach(function(elem, index) {
newObj[index] = elem;
});
return res.json(newObj); //<---this line builds the JSON response
});
promise.catch(function (err) {
return res.sendStatus(500);
});
});
请注意,在两个选项中,您都不会仅仅因为无法实现而最终获得所需的格式,而是两个选项都摆脱了数组语法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句