Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var pool = require('./connect').pool;
- var promise = require('promise');
- var shouldAbort = function(err) {
- if (err) {
- console.error('Error in transaction', err.stack);
- pool.connect(function(err,client,done){
- client.query('ROLLBACK', function(err) {
- if (err) {
- console.error('Error rolling back client', err.stack);
- }
- // release the client back to the pool
- done();
- })
- })
- pool.end();
- }
- return !!err;
- }
- module.exports.insert = function DateInstert(msg,na,nameChat){
- pool.connect(function(err,client,done){
- console.log("I make insert to db");
- client.query('BEGIN', function(err) {
- if (shouldAbort(err)) return;
- client.query('INSERT INTO public.users("nickname") VALUES($1) RETURNING
- id', [na], function(err, res) {
- if (shouldAbort(err )) return;
- var insertPhotoText = 'INSERT INTO public.chat("nameChat", user_id)
- VALUES ($1, $2)';
- var insertPhotoValues = [nameChat,res.rows[0].id];
- var insertmsg = 'INSERT INTO public.message(user_id_send,
- user_id_get,group_id, text) VALUES ($1, $2, $3, $4)';
- var insertValue = [4, 2, 0, msg];
- client.query(insertPhotoText, insertPhotoValues, function(err, res) {
- if (shouldAbort(err)) return;
- client.query(insertmsg, insertValue, function (err, res) {
- if (shouldAbort(err)) return;
- client.query('COMMIT', function (err) {
- if (err) {
- console.error('Error committing transaction', err.stack)
- }
- done();
- })
- })
- })
- })
- })
- })
- }
- $(document).on('click','button',function(){
- var message = $('input').val();
- socket.emit('message',message);
- $('input').val(null);
- });
- });
- socket.on('message', async function (msg) {
- InData.insert(msg, name, "Sex"); //вставляю данные на сервер
- socket.emit('messageToclient',con.newSelect.then(function (res) {
- return res; //событие для отправки на клиент данных, через запрос к бд.
- }));
- Ответ был прост)
- socket.on('message', async function (msg) {
- await InData.insert(msg, name, "Sex");
- socket.emit('messageToClient', await con.SelMsg());
- socket.broadcast.emit('messageToClient', await con.SelMsg());
- });
Add Comment
Please, Sign In to add comment