简体   繁体   English

我应该如何将同步node.js模块与mysql,套接字函数一起使用?

[英]how should I apply sync node.js module with mysql, socket functions?

I'm trying to understand and use sync-npm module, but not sure how to change my functions below to match sync format... ( https://www.npmjs.com/package/sync ) 我正在尝试了解和使用sync-npm模块,但不确定如何在下面更改我的功能以匹配同步格式...( https://www.npmjs.com/package/sync

Basically I'm trying to use input data (which is formed as a list in client side) I receive from frontend(client) and send it to node.js via socket. 基本上,我试图使用从frontend(client)接收的输入数据(在客户端以列表形式形成),然后通过套接字将其发送到node.js。 I tried to store it in my global variable 'query', but I learned that it doesn't get updated. 我试图将其存储在全局变量“查询”中,但是我知道它没有更新。 So when I tried to print 'query' outside of socket function, it doesn't work. 因此,当我尝试在套接字函数之外打印“查询”时,它不起作用。

It sounds like I should use sync module, but I'm not quite sure how to implement that in my code...If anyone could give me an idea how to change my functions below, it would be great..thanks! 听起来我应该使用同步模块,但是我不太确定如何在我的代码中实现它……如果有人可以在下面更改我的功能,请给我一个主意,谢谢。

Receiving input data from frontend and sending it to node.js via socket 从前端接收输入数据,并通过套接字将其发送到node.js

var server = app.listen(3001);
var socket = require('socket.io');
var io = socket(server);
var query = []

// Register a callback function to run when we have connection
io.sockets.on('connection',newConnection);
function newConnection(socket){
  console.log('new connection: ' + socket.id);
  socket.on('search', newSearch);
  function newSearch(final){
    query.push(final)

    console.log(query[0]);
    console.log(Array.isArray(query[0]));  //returns True
    console.log(query[0][0]);    // this also works
    console.log(query[0][1]);
  }
}
console.log('print');  
console.log(query);    
// this only gets printed in the beginning as an empty array

Ultimately, I'm parsing that list of input data and concat into my sql select phrase. 最终,我将输入数据列表和concat解析为sql select短语。 Below is my DB portion code: 下面是我的数据库部分代码:

  var mysql      = require('mysql');
  var connection = mysql.createConnection({
    host     : '~~~',
    user     : '~~~',
    password : '~~~',
    database : '~~~'
  });

  connection.connect();
  console.log('mysql connected');

  var sql = 'select' + '*' + 'from EBN ';
  //ideally data in the 'query' list will concat with this sql string

  connection.query(sql,function(err,rows,fields){
    if(err){
      console.log(err);
    }
    else{
      fs.writeFileSync('search.json', JSON.stringify(rows), 'utf8');
    }
});

Firstly, you should wrap the code that does the query execution in a function, something like 首先,您应该将执行查询的代码包装在一个函数中,例如

 var mysql = require('mysql'); var connection = mysql.createConnection({ host : '~~~', user : '~~~', password : '~~~', database : '~~~' }); connection.connect(); console.log('mysql connected'); function executeQuery(query) var sql = 'select' + '*' + 'from EBN '; //do something with query and add it to the sql string connection.query(sql,function(err,rows,fields){ if(err){ console.log(err); } else{ fs.writeFileSync('search.json', JSON.stringify(rows), 'utf8'); } } } 

This is necessary because you don't want to execute a query right after starting the server, but only after you received the query message via socket. 这是必要的,因为您不希望在启动服务器后立即执行查询,而只是在通过套接字收到查询消息之后才执行查询。 So now you can call executeQuery(final) in your socket message handler. 因此,现在您可以在套接字消息处理程序中调用executeQuery(final)

You should learn how asynchronous programming and callbacks works in nodejs/javascript, and why you should use it as much as possible for server applications (eg use fs.writeFile instead of writeFileSync ). 您应该了解异步编程和回调如何在nodejs / javascript中工作,以及为什么应在服务器应用程序中尽可能多地使用它(例如,使用fs.writeFile而不是writeFileSync )。 You can use packages like sync to make life easier for you, but only when you know exactly what you want to do. 您可以使用同步等程序包来简化生活,但前提是您确切地知道自己想做什么。 Just throwing something with the name 'sync' in it at a problem that might be caused by asynchronicity is not going to work. 仅在可能由异步性引起的问题中抛出名称为“ sync”的东西是行不通的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM