http://127.0.0.1:3000/api/events/user/id/1
我得到這樣的結果:如何在Node.js(express)上返回由{}代替[]而不是[]的json如果我對我的node.js(EXPRESS)API curl執行了curl請求,則返回使用knex和postgresql的查詢結果
[{"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:[email protected]: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對象名單現在回來了嗎?非常感謝您
編輯:這解決了我的問題,最終的格式爲{ 「行」:[{ROW1},{ROW2}等]}
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:{}}不適合? – iovoid