1

I have an api which return data structured like this :

//code return response.status(200).json(startdate)

results data:

[
    "2020-01-16",
    "2020-01-18",
]

i want that this api to return result like this:

Code: 200
Content:
{
    "availableDates": [
      "2017-11-24",
      "2017-11-27"
    ],
    "status": "SUCCESS",
    "message": ""
}

this is my full code where i get the data as array

app.get('/api/getBusyDays',(request, response) =>{
    odoo.connect(function (err) {



        console.log(' OdooStartDate' + dateTimeStartUsed + 'OdooStopdate' + dateTimeEndUsed);
        var params1 = [];
        params1.push(inParams1);

        console.log(' search params '+ JSON.stringify(params1));
        odoo.execute_kw('calendar.event', 'search_read', params1, function (err, value) {
            if (err) { return console.log(err) }
            if(value) {
                if (value.length > 0) {


                    value.forEach(function(a) {
                        a.start_datetime = moment(a.start_datetime).format('YYYY-MM-DD');
                        a.stop_datetime = moment(a.stop_datetime).format('YYYY-MM-DD');
                    });

                    const startdate = [...new Set(value.map(val => val.start_datetime))];

                    startdate.sort();


                   // return response.status(200).json( value)

                    return response.status(200).json(startdate)
                }
            }
2
  • 1
    So what's the problem? Commented Jan 16, 2020 at 13:58
  • don't know how to convert Commented Jan 16, 2020 at 14:01

2 Answers 2

2

you can just create an object like this:

let arrVal = [
  "2017-11-24",
  "2017-11-27"
];
// return Object or your framework (Express or KOA or) response Object
console.log({
  "Code": 200,
  "Content": {
    "availableDates": arrVal,
    "status": "SUCCESS",
    "message": ""
  }
})

Update:

Based on the comments on this answer, modify your response so it looks like this:

app.get('/api/getBusyDays',(request, response) =>{
    odoo.connect(function (err) {



        console.log(' OdooStartDate' + dateTimeStartUsed + 'OdooStopdate' + dateTimeEndUsed);
        var params1 = [];
        params1.push(inParams1);

        console.log(' search params '+ JSON.stringify(params1));
        odoo.execute_kw('calendar.event', 'search_read', params1, function (err, value) {
            if (err) { return console.log(err) }
            if(value) {
                if (value.length > 0) {


                    value.forEach(function(a) {
                        a.start_datetime = moment(a.start_datetime).format('YYYY-MM-DD');
                        a.stop_datetime = moment(a.stop_datetime).format('YYYY-MM-DD');
                    });

                    const startdate = [...new Set(value.map(val => val.start_datetime))];

                    startdate.sort();


                   // return response.status(200).json( value)

                    return response.status(200).json({"code": 200, "content": {"availableDates": startdate, "status": "SUCCESS", "message": ""}})
                }
            }
Sign up to request clarification or add additional context in comments.

4 Comments

sorry i reformulated my question maybe it was not clear the idea
@Alia do you have access to the API code base? and which framework are you using?
i cant add all the code ...but the idea is that api works well , just i want to have result not like array but json with status inside and message
if you want the response to be like this, then you have to modify the response to be an object and change the frontend implementation, response.status(200) only modify the response header status code and it doesn't create an object.
1

You can create an empty object and set properties to it and send it as response from ur node api service.

let data = [
    "2017-11-24",
    "2017-11-27"
];

let response = [];
response.data = data;
response.code = 200;
response.status = 'success';
response.message = 'Mission Successful';

return response;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.