Object.keys() not working in class - javascript

I have a class which reads a LokiJS database upon initialization and saves the database in an object, whereby a collection corresponds to a name. I want to have a separate array which holds just the names. I would normally do this with Object.keys(obj), but in my case that returns an empty array.
The code I so far have is:
class Barbot extends EventEmitter{
constructor(HOST, PORT, ioUID, lcUID) {
super();
var self = this;
// lokijs databases
this.dbCocktails = new loki('./src/JavaScript/Cocktails.json');
this.dbBottles = new loki('./src/JavaScript/Bottles.json');
//parameters
this.cocktails = {};
this.cocktailNames = [];
async.parallel({
db1 : function(callback){
self.dbCocktails.loadDatabase({}, function(){
callback(null);
});
},
db2 : function(callback){
self.dbBottles.loadDatabase({}, function(){
callback(null);
});
}
}, function(err, results) {
// initialize some objects
self.bottles = self.dbBottles.getCollection('Flaschen');
var cocktailArray = self.dbCocktails.listCollections();
for (let i = 0; i < cocktailArray.length; i++) {
self.cocktails[cocktailArray[i].name] = self.dbCocktails.getCollection(cocktailArray[i].name);
}
self.cocktailNames = Object.keys(self.cocktails); // somehow that doesn't work here!
})
}
This class is then imported from a normal JS-script which is run from a node-webkit app (is this maybe the problem? Does HTML have a problem with the class definition?). So that when I initialize this class and then log the Barbot.cocktails and Barbot.cocktailNames, the cocktails is what I would expect but the cocktailNames are empty. When I run the same code outside of a class, everything works as it should and the array of the names is filled with the names of my cocktails. Can someone help me figure out where my problem lies??
Edit: Here's the Code I use to initiate the class:
var Bb = require('./src/JavaScript/BarbotClass');
var HOST = 'localhost',
PORT = 4223,
ioUID = "wjY",
lcUID = "vbn";
var Barbot = new Bb(HOST, PORT, ioUID, lcUID);
console.log(Barbot)
console.log(Barbot.cocktailNames); // Array[0]
second Edit:
So when I log the Barbot object from this everything seems to be initialized just fine. But when I try to access parameters, they seem not to be the right ones. E.g. it looks like it would access the protoype of the parameters?? I included a picture of how the console output looks like.

Related

Doesn't add records into PouchDB when used same function over again

I'm trying to create a database with "users" and their data in it. Strangely it doesn't put() new variables in it when I try to for the third time. To do all this I create a local database dblocal and replicate this DB to the remote db called dbremote. At first I create a document with one variable.
function newuser() {
if (window.document.consent_form.consent_to_share.value) {
var id = "p" + Date.now() + "-" + Math.floor(Math.random() * 10000);
var dblocal = new PouchDB(id);
var consenttoshare = window.document.consent_form.consent_to_share.value;
document.cookie = id;
var dbremote = 'http://localhost:5984/experiment';
dblocal.put({
_id: id,
consent: consenttoshare
});
dblocal.replicate.to(dbremote, {live: true});
}
}
This all worked well, in another js file I'm trying to add a variable to the same document by executing the following function putdb(). Im doing this in the following way (as said in their documentation is the right way):
function putdb () {
if (document.cookie){
var id = document.cookie;
var loggedin = "True";
var dblocal = new PouchDB(id);
dblocal.get(id).then(function (doc) {
doc.loggedin = loggedin;
return dblocal.put(doc);
}).then(function () {
return dblocal.get(id);
}).then(function (doc) {
console.log(doc);
var dbremote = 'http://localhost:5984/experiment';
dblocal.replicate.to(dbremote, {live: true});
});
}
}
This succesfully added the variable loggedin to the document as I wanted. However upon trying to add information to this document for the third time (again in another js file), nothing happens. I used exactly the same approach as before but only use different variables.
function putdb (checked) {
if (document.cookie) {
var id = document.cookie;
var checkedlist = [];
for (i = 0; i < checked; i++) {
checkedlist.push($("input[type=checkbox]:checked")[i].value)
}
var playlistname = document.getElementById("playlistname").value;
var dblocal = new PouchDB(id);
dblocal.get(id).then(function (doc) {
doc.checkedlist = checkedlist;
doc.playlistname = playlistname;
return dblocal.put(doc);
}).then(function () {
return dblocal.get(id);
}).then(function (doc) {
console.log(doc);
var dbremote = 'http://localhost:5984/experiment';
dblocal.replicate.to(dbremote, {live: true});
});
}
}
I checked all variables, they are correct.
I tried plain text variables.
The script does run.
I tried to add information to the document the way I did the first time.
None of all this seems to add another variable to the document as I wanted in the last function. I think it has to do with the way pouchDB works which I don't know. help is much appreciated!
There are a number of problems in your code that results in bad usage of PouchDB, and may lead to problems.
First of all, it does not make a lot of sense to give your document the same id as the name of your database. Assuming you want a one database per user approach, there are two approaches you can follow.
Multiple document approach
You can instead make multiple documents within the same database with different id's. For instance, your 'consent' information may be stored like this:
var id = "p" + Date.now() + "-" + Math.floor(Math.random() * 10000);
let dblocal = new PouchDB(id);
document.cookie = id;
let dbremote = 'http://localhost:5984/experiment';
dblocal.put({
_id: "consent",
consent: window.document.consent_form.consent_to_share.value
});
dblocal.replicate.to(dbremote, {live: true});
While your playlist information is stored like this:
dblocal.put({
_id: "playlist",
name: playlistname,
itemsChecked: checkedlist
});
Single-document approach
The second option is to store a single document containing all the information you want to store that is associated to a user. In this approach you will want to fetch the existing document and update it when there is new information. Assuming you named your document global-state (i.e. replace "consent" in the first code snippet with "global-state"), the following code will update a document:
dblocal.get("global-state").then((doc)=>{
doc.loggedIn = true; // or change any other information you want
return dblocal.put(doc);
}).then((response)=>{
//handle response
}).catch((err)=>{
console.log(err);
});
Furthermore, you should only call the
dblocal.replicate.to(dbremote, {live: true});
function once because the 'live' option specifies that future changes will automatically be replicated to the remote database.

Create index on already existing objectStore

As an example on basic setup one index is created.
db.onupgradeneeded = function(event) {
var db = event.target.result;
var store = db.createObjectStore('name', { keyPath: 'id' });
store.createIndex('by name', 'name', { unique: false });
};
Question:
Is it possible to create/append more indexes to the same objectStore on the future versionupdate? Since if I try:
db.onupgradeneeded = function(event) {
var db = event.target.result;
var store = db.createObjectStore('name', { keyPath: 'id' });
store.createIndex('by newName', 'newName', { unique: false });
};
It throws an error that current objectStore does already exist. An if I try to create store reference using transaction:
db.onupgradeneeded = function(event) {
var db = event.target.result;
var store = db.transaction('name', 'readwrite').objectStore('name');
store.createIndex('by newName', 'newName', { unique: false });
};
It throws that version change transaction is currently running
Yes it is possible. It can be a bit confusing at first. You want to get the existing object store via the implicit transaction created for you within onupgradeneeded. This is a transaction of type versionchange which is basically like a readwrite transaction but specific to the onupgradeneeded handler function.
Something like this:
var request = indexedDB.open(name, oldVersionPlusOne);
request.onupgradeneeded = myOnUpgradeNeeded;
function myOnUpgradeNeeded(event) {
// Get a reference to the request related to this event
// #type IDBOpenRequest (a specialized type of IDBRequest)
var request = event.target;
// Get a reference to the IDBDatabase object for this request
// #type IDBDatabase
var db = request.result;
// Get a reference to the implicit transaction for this request
// #type IDBTransaction
var txn = request.transaction;
// Now, get a reference to the existing object store
// #type IDBObjectStore
var store = txn.objectStore('myStore');
// Now, optionally inspect index names, or create a new index
console.log('existing index names in store', store.indexNames);
// Add a new index to the existing object store
store.createIndex(...);
}
You also will want to take care to increment the version so as to guarantee the onupgradeneeded handler function is called, and to represent that your schema (basically the set of tables and indices and properties of things) has changed in the new version.
You will also need to rewrite the function so that you only create or make changes based on the version. You can use event.oldVersion to help with this, or things like db.objectStoreNames.contains.
Something like this:
function myOnUpgradeNeeded(event) {
var is_new_db = isNaN(event.oldVersion) || event.oldVersion === 0;
if(is_new_db) {
var db = event.target.result;
var store = db.createObjectStore(...);
store.createIndex('my-initial-index');
// Now that you decided you want a second index, you also need
// to do this for brand new databases
store.createIndex('my-second-new-index');
}
// But if the database already exists, we are not creating things,
// instead we are modifying the existing things to get into the
// new state of things we want
var is_old_db_not_yet_current_version = !isNaN(event.oldVersion) && event.oldVersion < 2;
if(is_old_db_not_yet_current_version) {
var txn = event.target.transaction;
var store = txn.objectStore('store');
store.createIndex('my-second-new-index');
}
}
Pay close attention to the fact that I used event.target.transaction instead of db.transaction(...). These are not at all the same thing. One references an existing transaction, and one creates a new one.
Finally, and in addition, a personal rule of mine and not a formal coding requirement, you should never be using db.transaction() from within onupgradeneeded. Stick to modifying the schema when doing upgrades, and do all data changes outside of it.

How to query relational data in Parse JavaScript

I am quite new to Parse.
I have a database set up using this code:
var Class = Parse.Object.extend("Class");
var Team = Parse.Object.extend("Team");
var Student = Parse.Object.extend("Student");
var newClass = new Class();
newClass.set("name", className);
newClass.set("code", classCode);
newClass.set("creator", currentUser);
var classACL = new Parse.ACL(currentUser);
classACL.setPublicReadAccess(true);
newClass.setACL(classACL);
newClass.save();
for (var i = 0; i < teamNames.length; i++) {
var team = new Team();
team.set("name", teamNames[i]);
var teamACL = new Parse.ACL(currentUser);
teamACL.setPublicReadAccess(true);
team.setACL(teamACL);
team.save();
for (var j = 0; j < studentNames[i].length; j++) {
if (studentNames[i][j]) {
var student = new Student();
student.set("name", studentNames[i][j]);
student.set("parent", team);
student.save();
}
}
team.set("parent", newClass);
team.save();
}
newClass.save(null, {
success: function(newClass) {
//success
},
error: function(newClass, error) {
//fail
}
});
Here Class, Team, and Student are modeled as one-to-many relationships.
Now when a student signs up for the class using his or her own user account, the corresponding Student's user column is set to the current user.
Then I want to list all the classes whose creator OR one of its student's user column (if exists) equals to currentUser.
How do I create such a query referencing multiple classes in Parse (or how can I optimize the database so that such a query can be made as efficient as possible -- without having to create two separate queries?)
Any help is appreciated.
Clarification:
I knew that I could do an or query as described in Parse docs (I should have stated this in the question), however, my question is about doing so on relational data (defined by a pointer type property to parent). Here I need user be a property of a Student instance, which belongs to Team, and then to Class, and I'd like to filter only Class objects that has either its creator property or one of its grandchildren's (an instance of Student) user property equal to the currentUser, effectively listing only the classes that you created or are registered as a student.
Since the current database schema is having nested Pointers, there is no easy way to achieve this without adjusting it.
Database Schema
In Class class, add a Relation or Array field to contain references to Student/User objects. If you use User as object pointer, we wouldn't need to look up for Student at first.
Query
I assume that you have students as new Array field in Class class. students contains User objects.
var user = Parse.User.current();
var studentQuery = new Parse.Query(Class);
var creatorQuery = new Parse.Query(Class);
studentQuery.equalTo("students", user);
creatorQuery.equalTo("creator", user);
var query = Parse.Query.or(studentQuery, creatorQuery);
query.find().then(function(objects){
// Proceed with the results
},function(error){
// Handle error
});
Ok, what you want to do in an OR query with an internal subquery. One call to parse and you can filter the student properties using the subquery.
var studentQuery = new Parse.Query(Student);
studentQuery.equalTo("user", Parse.User.current());
var firstQuery = new Parse.Query(Class);
firstQuery.matchesQuery("student", studentQuery);
var secondQuery = new Parse.Query(Class);
secondQuery.equalTo("creator", Parse.User.current());
var mainQuery = Parse.Query.or(firstQuery, secondQuery);
mainQuery.find({
success: function(results) {
// results contains a list of Classes where the user is either the creator or the user property of the student (if available)
},
error: function(error) {
// There was an error.
}
});

Store object dynamically in object

This question might sound stupid or a little too simple but I seriously got stuck.
I have a node.js app and need to store clients to be able to send information from a specific client to a specific other client.
var clients = { };
var chat = io.of('/chat')
.on('connection', function(socket) {
socket
.on('ehlo', function(data) {
// mysql queries here etc to get client's data,
// for example session_id, customer name, ...
// got stuck here, need to save the socket in customers object like:
clients.(customer + data.get_customer_id) = { socket: socket };
I later need to be able to access this property from a different scope etc.
Basically I lack an idea of how these abstract methods can be implemented:
clients.addClient({ id: 123, socket: socket});
And later find them by:
x = clients.findById(123);
// x now should be { id: 123, socket: socket}
Well, you can define a clients module first.
//clients.js
"use strict";
var clients = {
_db: []
};
clients.findById = function(id){
for(var i=0; i < this._db.length; i++){
var client = this._db[i];
if(client.id === id){
return client;
}
}
return null;
};
clients.addClient = function(client) {
if(!this.findById(client.id)){
this._db.push(client);
}
};
module.exports = clients;
The way Node.js works it will cache your module after it's initialized. This after you use it for the first time. This means that every time you require it, you will get the same clients object. So now you can do this in a hypothetical module1.js:
//module1.js
var clients = require('./clients');
clients.addClient(newClient);
And later in another module you can gain access to the same data structure:
//module2.js
var clients = require('./clients');
var client = clients.findById(id);
And that's it, you can gain access to your same data structure from different contexts. The point here is that all modules will share the same clients object.

Chrome Extension with Database API interface

I want to update a div with a list of anchors that I generate from a local database in chrome. It's pretty simple stuff, but as soon as I try to add the data to the main.js file via a callback everything suddenly becomes undefined. Or the array length is set to 0. ( When it's really 18. )
Initially, I tried to install it into a new array and pass it back that way.
Is there a setting that I need to specify in the chrome manifest.json in order to allow for communication with the database API? I've checked, but all I've been able to find was 'unlimited storage'
The code is as follows:
window.main = {};
window.main.classes = {};
(function(awe){
awe.Data = function(opts){
opts = opts || new Object();
return this.init(opts);
};
awe.Data.prototype = {
init:function(opts){
var self = this;
self.modified = true;
var db = self.db = openDatabase("buddy","1.0","LocalDatabase",200000);
db.transaction(function(tx){
tx.executeSql("CREATE TABLE IF NOT EXISTS listing ( name TEXT UNIQUE, url TEXT UNIQUE)",[],function(tx,rs){
$.each(window.rr,function(index,item){
var i = "INSERT INTO listing (name,url)VALUES('"+item.name+"','"+item.url+"')";
tx.executeSql(i,[],null,null);
});
},function(tx,error){
});
});
self._load()
return this;
},
add:function(item){
var self = this;
self.modified = true;
self.db.transaction(function(tx){
tx.executeSql("INSERT INTO listing (name,url)VALUES(?,?)",[item.name,item.url],function(tx,rs){
//console.log('success',tx,rs)
},function(tx,error){
//console.log('error',error)
})
});
self._load()
},
remove:function(item){
var self = this;
self.modified = true;
self.db.transaction(function(tx){
tx.executeSql("DELETE FROM listing where name='"+item.name+"'",[],function(tx,rs){
//console.log('success',tx,rs)
},function(tx,error){
//console.log('error',tx,error);
});
});
self._load()
},
_load:function(callback){
var self = this;
if(!self.modified)
return;
self.data = new Array();
self.db.transaction(function(tx){
tx.executeSql('SELECT name,url FROM listing',[],function(tx,rs){
console.log(callback)
for(var i = 0; i<rs.rows.length;i++)
{
callback(rs.rows.item(i).name,rs.rows.item(i).url)
// var row = rs.rows.item(i)
// var n = new Object()
// n['name'] = row['name'];
// n['url'] = row['url'];
}
},function(tx,error){
//console.log('error',tx,error)
})
})
self.modified = false
},
all:function(cb){
this._load(cb)
},
toString:function(){
return 'main.Database'
}
}
})(window.main.classes);
And the code to update the list.
this.database.all(function(name,url){
console.log('name','url')
console.log(name,url)
var data = []
$.each(data,function(index,item){
try{
var node = $('<div > '+item.name + '</div>');
self.content.append(node);
node.unbind();
node.bind('click',function(evt){
var t = $(evt.target).attr('href');
chrome.tabs.create({
"url":t
},function(evt){
self._tab_index = evt.index
});
});
}catch(e){
console.log(e)
}
})
});
From looking at your code above, I notice you are executing "self._load()" at the end of each function in your API. The HTML5 SQL Database is asynchronous, you can never guarantee the result. In this case, I would assume the result will always be 0 or random because it will be a race condition.
I have done something similar in my fb-exporter extension, feel free to see how I have done it https://github.com/mohamedmansour/fb-exporter/blob/master/js/database.js
To solve a problem like this, did you check the Web Inspector and see if any errors occurs in the background page. I assume this is all in a background page eh? Try to see if any error occurs, if not, I believe your encountering a race condition. Just move the load within the callback and it should properly call the load.
Regarding your first question with the unlimited storage manifest attribute, you don't need it for this case, that shouldn't be the issue. The limit of web databases is 5MB (last I recall, it might have changed), if your using a lot of data manipulation, then you use that attribute.
Just make sure you can guarantee the this.database.all is running after the database has been initialized.

Categories

Resources