javascript - get subdomain and query database for results - Meteor -
i pretty new meteor now.
i want to
- get sub-domain of url
- check if client exists matching sub-domain
- if client exists query database , results (say client settings) database.
i sure piece of cake if use mongodb, however, have move existing application (built on php) has mysql backend.
i have found package numtel:mysql meteor , have added project.
here source code written far:
if(!session.get('client_details')) { var hostnamearray = document.location.hostname.split('.'); if(hostnamearray[1] === "localhost" && hostnamearray[2] === "local") { var subdomain = hostnamearray[0]; } if(subdomain) { currentclientdetails = new mysqlsubscription('getclientdetailsfromurl', subdomain).reactive(); tracker.autorun(function() { if(currentclientdetails.ready()) { if(currentclientdetails.length > 0) { var clientdetails = currentclientdetails[0]; session.setpersistent('client_details', clientdetails); var clientid = clientdetails.id; if(!session.get('client_settings')) { clientsettings = new mysqlsubscription('clientsettings', clientid).reactive(); tracker.autorun(function() { if(clientsettings.ready()) { if(clientsettings.length > 0) session.setpersistent('client_settings', clientsettings[0]); else session.setpersistent('client_settings', {}); } }); } } } }); } }
the session.setpersistent
comes u2622:persistent-session store sessions on client side , here publish statement:
meteor.publish("getclientdetailsfromurl", function(url) { if(typeof url === undefined) { return; } var clientdetails = meteor.readlivedb.select( 'select * clients client_url = "'+ url +'"', [{table: 'clients'}] ); return clientdetails; }); meteor.publish("clientsettings", function(clientid) { if(typeof clientid === undefined) { throw new error('clientid cannot null'); return; } var clientsettings = meteor.readlivedb.select( 'select * client_settings client_id = ' + clientid, [{ table: 'client_settings' }]); return clientsettings; });
and database initiated
meteor.readlivedb = new livemysql(<mysql settings host, user, passwords, etc>);
problem
i client_details
session successfully, however, cannot client_settings
session. end error:
exception tracker recompute function: error: subscription failed! @ array.mysqlsubscription (mysqlsubscription.js:40) @ app.js?6d4a99f53112f9f7d8eb52934c5886a2b7693aae:28 @ tracker.computation._compute (tracker.js:294) @ tracker.computation._recompute (tracker.js:313) @ object.tracker._runflush (tracker.js:452) @ onglobalmessage (setimmediate.js:102)
i know code messy , lot better, please suggestions welcome
Comments
Post a Comment