Guest User

Untitled

a guest
Jan 16th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. var pool = require('./connect').pool;
  2. var promise = require('promise');
  3.  
  4.  
  5.  
  6. var shouldAbort = function(err) {
  7. if (err) {
  8. console.error('Error in transaction', err.stack);
  9. pool.connect(function(err,client,done){
  10. client.query('ROLLBACK', function(err) {
  11. if (err) {
  12. console.error('Error rolling back client', err.stack);
  13. }
  14. // release the client back to the pool
  15. done();
  16. })
  17. })
  18. pool.end();
  19. }
  20. return !!err;
  21. }
  22.  
  23. module.exports.insert = function DateInstert(msg,na,nameChat){
  24.  
  25. pool.connect(function(err,client,done){
  26.  
  27. console.log("I make insert to db");
  28. client.query('BEGIN', function(err) {
  29. if (shouldAbort(err)) return;
  30. client.query('INSERT INTO public.users("nickname") VALUES($1) RETURNING
  31. id', [na], function(err, res) {
  32. if (shouldAbort(err )) return;
  33.  
  34. var insertPhotoText = 'INSERT INTO public.chat("nameChat", user_id)
  35. VALUES ($1, $2)';
  36. var insertPhotoValues = [nameChat,res.rows[0].id];
  37.  
  38. var insertmsg = 'INSERT INTO public.message(user_id_send,
  39. user_id_get,group_id, text) VALUES ($1, $2, $3, $4)';
  40.  
  41.  
  42. var insertValue = [4, 2, 0, msg];
  43.  
  44. client.query(insertPhotoText, insertPhotoValues, function(err, res) {
  45. if (shouldAbort(err)) return;
  46.  
  47. client.query(insertmsg, insertValue, function (err, res) {
  48. if (shouldAbort(err)) return;
  49.  
  50. client.query('COMMIT', function (err) {
  51.  
  52. if (err) {
  53. console.error('Error committing transaction', err.stack)
  54. }
  55. done();
  56. })
  57. })
  58. })
  59.  
  60. })
  61. })
  62. })
  63. }
  64.  
  65. $(document).on('click','button',function(){
  66.  
  67. var message = $('input').val();
  68. socket.emit('message',message);
  69. $('input').val(null);
  70. });
  71. });
  72.  
  73. socket.on('message', async function (msg) {
  74. InData.insert(msg, name, "Sex"); //вставляю данные на сервер
  75. socket.emit('messageToclient',con.newSelect.then(function (res) {
  76. return res; //событие для отправки на клиент данных, через запрос к бд.
  77. }));
  78.  
  79. Ответ был прост)
  80.  
  81.  
  82.  
  83. socket.on('message', async function (msg) {
  84. await InData.insert(msg, name, "Sex");
  85. socket.emit('messageToClient', await con.SelMsg());
  86. socket.broadcast.emit('messageToClient', await con.SelMsg());
  87. });
Add Comment
Please, Sign In to add comment