Javascript对象在记录集上变为未定义

时间:2014-06-05 14:13:17

标签: javascript ms-access

我有一个access数据库,我想将数据导入javascript。

这是我的代码:

function Flight(){
    this.number;
    this.day;
    this.updateDate;
    this.html;
}

var dbPath = "mypath\\flight_bdd.mdb";
var flights = [];

function executeRequest(request){
    //get datas
    var adoConn = new ActiveXObject("ADODB.Connection");
    var adoCmd = new ActiveXObject("ADODB.Command");

    adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + dbPath + "'");
    adoCmd.ActiveConnection = adoConn;

    var adOpenDynamic=2;
    var adLockOptimistic=3;

    var rs = new ActiveXObject("ADODB.Recordset");
    rs.open(request, adoConn, adOpenDynamic, adLockOptimistic);

    return rs;
}

function loadFlightsFromDatabase(){

    //get datas
    var rs = executeRequest("SELECT * FROM flight_data");

    //empty flight array
    flights = [];

    //create flights
    var i = 0
    while(!rs.eof){
            flights[i] = new Flight();

            //set flight data
            flights[i].number = rs.fields("flight_number"); 

            console.log(flights[i].number);
            rs.MoveNext();
            console.log(flights[i].number);
            i++;
    }

}

第一个控制台输出返回航班号,第二个返回未定义。 我认为当我接下来移动时,记录集的值会在我的对象中更新,有没有办法阻止它?

2 个答案:

答案 0 :(得分:0)

我认为您的连接超出了范围并且正在关闭。尝试创建断开连接的记录集:

var adOpenStatic = 3;    
var adUseClient = 3;
var adLockBatchOptimistic = 4; 

var rs = new ActiveXObject("ADODB.Recordset");
rs.CursorLocation = adUseClient;
rs.open(request, adoConn, adOpenStatic, adLockBatchOptimistic);
rs.ActiveConnection = null;
adoConn.Close;

return rs;

但是那样说,rs.MoveNext();不应该影响对象的已分配属性(除非它是引用类型)。什么类型.number - 你可以展示飞行的结构吗?

答案 1 :(得分:0)

我找到了解决方案,

我连接了我的记录集

flights[i].number = rs.fields("flight_number") + "";

它有效!!