0

こんにちは、nodejs と接続された mysql データベースを新しく学習しています。選択クエリの結果を何らかのタイプの変数に保存したいのですが、できません。

var campGround = [];


console.log("Select Statement started....");
    con.connect(function(error){

        if(!error){
            console.log("Connected");
            var sql = "select * from campgrounds";
            con.query(sql,function(err,result,field){
                if (!err) {
                    // console.log(JSON.parse(result));
                    for(var i =0;i<result.length;i++)
                    {
                        try {
                            // console.log(result[i]);
                            setCampground(result[i]);
                            // campGround.push(result[i]);
                        } catch (error) {
                            console.log(error.message);
                        }

                    }
                }
                else{
                    console.log("Error while selecting record from campground table. ");
                }
            });
        }else{

            console.log("Error DataBase Not Connected!!! select statement");
        }
    });

    function setCampground(value){
        this.campGround.push(value);
    }


    console.log("length after execution :: "+campGround.length);
    campGround.forEach(function(value){
        console.log("Campground array");
        console.log(value);
    });

上記のコードを実行してデバッグすると...selectステートメントはデータベースから3つのレコードを返します...しかし、それらを配列にプッシュすると...そして配列を印刷しても何も起こりません...助けてください

私を助けることができるものを見つけることができません。

4

2 に答える 2