Search code examples
javascriptrestapiweb-servicesodoo

Create rest api to retrieve data from odoo erp


I am creating a rest api service to get, put, delete data from odoo erp .

Here is my code :

const Promise = require('bluebird');
Promise.promisifyAll(require('node-odoo').prototype);

const Odoo = require('odoo-xmlrpc');


const odoo = new Odoo({
    url: 'zzzz',
    port: 'zz',
    db: 'zzzz',
    username: 'zzzz',
    password: 'zzz*'
});


var express = require('express'),
    app = express(),
    port = process.env.PORT || 3000;
this.router = express.Router();
app.listen(port);

console.log('todo list RESTful API server started on: ' + port);


this.router.get('/api/event/', (req, res) => {
    return getEvent(req, res);
});


app.get('/getEvent', (request, response) => {

        odoo.connect((err) => {
            if(err)  return console.log('Findeventlist error ' + err);
            console.log('Findeventlist connected ' );
            var inParams = [];
            inParams.push([]);
            inParams.push(['name' ])
            inParams.push(0)
            inParams.push(5)
            var params = [];
            params.push(inParams);
            odoo.execute_kw('calendar.event', 'search_read', params, function (err, value) {
                if (err) { return console.log(err) }
                if(value){
                    console.log( 'Value is ' +   response.status(200).json(value));

                    return response.status(200).json(value)
                }
            });
        });
        console.log(' odoo connected');


})

I got this error : Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Can you help me please where i'm wrong?


Solution

  • response.status(200).json(value) responds to the client and ends the transaction (just like response.end() or response.sendFile() etc).

    It must be called only once, but you execute it twice (once inside the console.log() then once "for real" the next line). That's why the headers are "already sent".

    Remove the useless response.json() from the console.log and log only the value you want to see.