Node js + Error: This socket was terminated by different code]: "EPIPE"

I am using nodejs and postgresql

I am trying to insert data via ajax call. Trying to do the following.

1. Insert the record in conversation table.
2. get the conversation table last insert id(primary key).
3. last insert id try to insert other table multiple times.

      

Mistake:

The request worked well. after insertion, this data on the front side does not work. When I try to refresh the page on the frontend side, I get the following error in the terminal "{[Error: This socket was terminated by different code]:" EPIPE "}"

I have added my following code.

var express = require('express');
var router = express.Router();
var pg = require('pg');
var client = require('../routes/database.js');
var cookieParser = require('cookie-parser');
var dateFormat = require('date-format');
var async = require('async');
var today = dateFormat(new Date());



router.post('/conversation/start', function(req, res) {
    //var partyId = sess.userId;
         var partyId = 20;
    var topic = "welcome";
    var msg = "test message";
    var to_user_details = JSON.parse([{"id":1},{"id":2},{"id":3},{"id":4},{"id":5}]);

    client.query('BEGIN', function(err, result) {
        if (err) {
            console.log(err);
            return rollback(client);
        }
        client.query('insert into conversation (party_id , topic, message, created_on) values (' + "'" + partyId + "'" + ',' + "'" + topic + "'" + ',' + "'" + msg + "'" + ',' + "'" + today + "'" + ') RETURNING id', function(err, result) {
            if (err) {
                console.log(err);
                return rollback(client);
            }
            var conversationId = result.rows[0].id;
            async.each(to_user_details, addToUsersConversation.bind(null, conversationId), function(err) {
                if (err) {
                    console.log(err);
                    return rollback(client);
                }
                client.query('COMMIT', client.end.bind(client));
                res.send("conversation added");
            });
        });

    });

});




var addToUsersConversation = function(conversationId, toUserDetails, cb) {
    client.query('insert into conversation_to (conversation_id , party_id, type) values (' + "'" + conversationId + "'" + ',' + "'" + toUserDetails.id + "'" + ',' + "'" + toUserDetails.type_flag + "'" + ')', function(err, result) {
        if (err)
            return cb(err);
        cb(null);
    });

};

      

+3


source to share





All Articles