vendredi 17 avril 2015

Object.keys returns constructor[property]

I'm working with node.js and sqlite on a little tool that will insert data into the database. The problems start because I don't want to know what the data is.


The data constructor will be similar to this



EventFormData = function(){
this.table = 'event';
this.eventID = 0;
this.title = $('#Title').val();
this.Start_Date = $('#StartDate').val();
this.Start_Time = $('#StartTime').val();
this.End_Date = $('#EndDate').val();
this.End_Time = $('#EndTime').val();
this.Location = $('#Place').val();
this.Notes = $('#Notes').val();
};


So every form I make the only code I need to add would be the data constructor for this form, This constructor is global because it is in a jade file and i need it else where.


Now the dbtools.js is as followed:



dbtools = (function () {
var InsertData = function(data){
database.serialize(function(){
var dataArray = [];
var valueString = "", typeString = "";
for(var i = 2; i < Object.keys(data).length; i++){
if(i < Object.keys(data).length-1){
valueString += "?, ";
}else{
valueString += "?";
}
}
console.log(Object.keys(data));

database.all("INSERT into event ("+Object.keys(data)+") values("+valueString+" )", dataArray);
});
};
return {
InsertData: InsertData
};
})();

module.exports = dbtools;


I would imagine this would work but the Object.keys(data) doesn't work right. it returns:



eventFormData[table]
eventFormData[eventID]
...etc.


Now by my understanding in this case Object.keys(data) should return:



table
eventID
...etc.


If I do the same call but the constructor is in the same file it does what I want.


EDIT I call dbtools.InsertData(data); wherever I require dbtools, (mainly in router.post calls)


What am I doing wrong here?


Aucun commentaire:

Enregistrer un commentaire