用于循环查询mysql的节点js不起作用

时间:2015-07-29 18:52:47

标签: javascript mysql node.js rest for-loop

在节点js中,运行for循环后,descriptp不包含任何内容,没有字段写入descrip数组,为什么?

undo

调试结果

var express = require('express');
var mysql = require('mysql');
var bodyParser = require("body-parser"); // Body parser for fetch posted data
var app = express()
var connection = mysql.createPool({
    host : 'xxx.xx.xxx.xx',
    user : 'admin',
    database :'admin_btb',
    password : 'password'
    });
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); // Body parser use JSON data

app.post('/test', function(req, res){
        var token = req.body.token;
        var data = req.body;
        console.log(data);
        var barcode = req.body.barcode;
        var price = {
            "number":0,
            "price":""
            };
             connection.query("SELECT token from device where token = ?",[token],function(err, rows, fields){
                 if(rows.length != 0){
                    console.log("total = "+barcode.length);
                    for( var i = 0,length = barcode.length; i < length; i++){
                        //if (err) throw err;
                        console.log(i);
                        console.log(barcode[i])
                        console.log("Debug 1");
                        connection.query("SELECT unitprice from product where barcode = ?",[barcode[i]],function(err, rows, fields){
                            console.log("Debug 2");
                            price["price"[i]] = rows;
                            console.log("Debug 3");
                            console.log("Debug 4");
                            console.log(rows);
                            //res.json(rows);

                        });

                     console.log("Debug 5");
                     }
                    console.log("Debug 6");
                  }else{
                    res.status(400).send("not have token"); 
                  }console.log("Debug 7");
            }); 
});
邮递员测试 http://i.imgur.com/2etpk5e 我想查询uniprice添加价格,但代码不对它进行排序。 我怎样才能解决我的问题?

0 个答案:

没有答案