Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nodejs + node-mysql (terlampau banyak nested)
04-11-2013, 09:20 PM,
Post: #1
Nodejs + node-mysql (terlampau banyak nested)
Rasa macam bodoh je buat gini... Tak ada cara lain ke?

Code:
connection.query("SELECT username FROM accounts WHERE username = " + adminName + " AND cookies = " + adminCookie + " AND accountType = 1", function(err, rows, fields)
{
    if (connection.escape(rows[0].username) == adminName)
    {
        connection.query('SELECT intv FROM contract WHERE no = ' + contract, function(err, rows, fields)
        {
            var interval = rows[0].intv;
            connection.query("INSERT INTO accounts (name, email, username, password, startDate, runningUntil, accountType, contract) VALUES (" + name + "," + email + "," + username + ",md5(" + 123456 +"), now(), DATE_ADD(NOW(), INTERVAL "+ interval +")," +   accountType + "," + contract + ")", function(err, rows, fields)
            {
                if (err)
                {
                    if (err.code == "ER_DUP_ENTRY")
                    {
                        io.sockets.emit('updateAddUser', "ER_DUP_ENTRY");
                    }
                    else
                    {
                        io.sockets.emit('updateAddUser', "ER_UNKNOWN");
                        console.log(err.code);
                    }
                }
                else
                {
                    //exec('useradd -s /bin/false -d /bin/null -c "dPanel User" -d /home/' + username + ' ' + username);
                    io.sockets.emit('updateAddUser', username + ' have been added');
                }
            });
        });
    }
    else
    {
        io.sockets.emit('updateAddUser', "ER_WRONG_ADMIN");
    }
});

kalau tengok betul-betul code tu, terlampau banyak sangat nested connection.query().. sebabnya nak guna value tu untuk digunakan kepada next connection.query()...

Soalan:

Kalau buat function, dia tak return value tu sebab async process, so, akan dapat undefined.. Buat callback (tak faham sangat callback, dah buat n tak jadi) pun dapat undefined. Jadi, camne nak solve ni? Sebab nak sangat result dari connection.query() tu di assignkan ke satu variable... Ke memang ini je cara yang ada?

p/s: sory, bodoh async OK
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  Nodejs + php (add linux user via jquery+socket.io security issue) ahmadfikrizaman 9 845 04-11-2013, 11:30 AM
Last Post: ahmadfikrizaman

Forum Jump: