I have nodejs-mongo setup with db configured as follows (only one entry shown here)
{
"filename":"type1.json","secs":72.4060092977,"platform":"mac","version":"1.3.0", "inputfile":"temp.mov"
},
Here are the mongo commands I am trying to replicate
db.perfR.distinct("platform") * (output: ["mac", "win"] ) *
db.perfR.distinct("version",{"platform":"win"}) * (output: ["1.3.0", "1.3.2"] ) *
db.perfR.find({"version":1.3.2,"platform":"win"},{"filename":1,"secs":1,"_id":0}) * (output: ["filename":"type1.json","secs":72.4060092977] ) *
So what I am trying to do is
for every platform
for every version
get filename
get secs
Here is the code I have written thus far
function createPlatformDataSets(callback){
var dbHost = "mongodb://mongo:27017/perfSample";
var mongodb = require('mongodb')
var platformDataSets = []
var platformq = "platform"
//get Instance of Mongoclient
var MongoClient = mongodb.MongoClient;
//Connecting to the Mongodb instance.
//Make sure your mongodb daemon mongod is running on port 27017 on localhost
MongoClient.connect(dbHost, function(err, db){
if ( err ) throw err;
//use the distinct() to retrive distinct platforms
db.collection("perfR").distinct(platformq,function(err, platResultSet){
if ( err ) throw err;
var maxPlatCnt = platResultSet.length // 1
if (maxPlatCnt == 0){
console.log("Bad PlatfQ Query")
callback(true)
}
var versionedPlatDataSet = 0
for (p=0; p < platResultSet.length; p++){
(function(index){
var platform = platResultSet[index]
var options = createOptions(platform);
//Get Versions
var versionq = "\"version\",{\"platform\":" + platform + "}"
console.log("Versionq::"+versionq)
var dataSets = [];
//var versions = ["1.3.0", "1.3.2"]; // (select disctinct(version) from cpu where platform = plat)
// Use distinct() to find distinct Versions
db.collection("perfR").distinct(versionq,function(err, verResultSet){
if ( err ) throw err;
var maxVerCnt = verResultSet.length // 2
if (maxVerCnt == 0){
db.close()
console.log("Bad Versionq Query")
callback(true)
}
var dataSetResponseCnt = 0
for ( v=0; v < verResultSet.length; v++){
(function(idx){
var dataq = "{platform:" + platform + ",version:" + version + "},{filename:1,secs:1,_id:0}"
// Use find() to find filename and secs for given version and platform
db.collection("perfR").find(dataq,function(err, dataResultSet){
if ( err ) throw err;
if (dataResultSet.length == 0){
console.log("Bad dataq Query")
callback(true)
}
//do something with filename and secs
dataSetResponseCnt++
if (maxVerCnt == dataSetResponseCnt){
var platformData = {"options":options, "labels":labels, "datasets":dataSets, "platform":platform}
platformDataSets.push(platformData)
if (versionedPlatDataSet == maxPlatCnt){
db.close()
callback(null,platformDataSets)
}
}
})
})(v)
}
versionedPlatDataSet++
})(p)
}
}
}
At "1" I am able to retrive distinct platforms
But at "2" I get verResultSet.length to be zero.
Can someone point to me what is wrong?
(PS: This is my first serious async problem with javascript so bear with my code. All suggestions are welcome :) )
you can use Promises. so for example your code is going to be something like this:
return loadPlatforms().then(function (res){
res.map(function(platform){
loadVersion(platform).then(...)
}
})
Your use of .distinct() isn't correct. You are passing a JSON-like string when the API actually takes two separate arguments (not including the callback). So the real code should in fact just be more or less what you originally showed:
var query = { platform: 'win' };
db.collection('perfR').distinct('version', query, function(err, verResultSet) {
// ...
});
Related
I've been scouring similar problems but haven't seem to have found a solution that quite works on my end. So I'm working on a Discord bot that takes data from a MongoDB database and displays said data in the form of a discord embedded message using Mongoose. For the most part, everything is working fine, however one little section of my code is giving me trouble.
So I need to import an array of both all available users and the "time" data of each of those users. Here is the block of code I use to import said data:
for (i = 0;i < totalObj; i++){
timeArray[i] = await getData('time', i);
userArray[i] = await getData('user', i);
}
Now this for loop references a function I made called getData which obtains the data from MongoDB by this method:
async function getData(field, value){
var data;
await stats.find({}, function(err, result){
if(err){
result.send(err);
}else{
data = result[value];
}
});
if(field == "user"){
return data.user;
}else if (field == "time"){
return data.time;
}else{
return 0;
}
So that for loop is where my errors currently lie. When I try to run this code and display my data through a discord message, I get this error and the message does not get sent:
(node:13936) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'time' of undefined
Now the strange thing is, this error does not happen every time. If I continue calling the command that triggers this code from my discord server, it's almost like a 50/50 shot if the command actually shows the message or instead gives this error. It is very inconsistent.
This error is confounding me, as the undefined part does not make sense to me. The objects that are being searched for in the mongoDB collection are definitely defined, and the for loop never exceeds the number of objects present. My only conclusion is that I'm doing something wrong with my asynchronous function design. I have tried altering code to use the getData function less often, or to not use awaits or asynchronous design at all, however this leaves my final discord message with several undefined variables and an eventual crash.
If anyone has any advice or suggestions, that would be very much appreciated. Just for reference, here is the full function that receives the data, sorts it, and prepares a string to be displayed on the discord server (though the error only seems to occur in the first for loop):
async function buildString(){
var string = "";
var totalObj;
var timeArray = [];
var userArray = [];
var stopSort = false;
await stats.find({}, function(err, result){
if(err){
result.send(err);
}else{
totalObj = result.length;
}
});
for (i = 0;i < totalObj; i++){
timeArray[i] = await getData('time', i);
userArray[i] = await getData('user', i);
}
while(!stopSort){
var keepSorting = false;
for(i = 0; i < totalObj ; i++){
var target = await convertTime(timeArray[i]);
for(j = i + 1 ; j < totalObj ; j++){
var comparison = await convertTime(timeArray[j]);
if(target > comparison){
//Switch target time with comparison time so that the lower time is up front
var temp = timeArray[i];
timeArray[i] = timeArray[j];
timeArray[j] = temp;
//Then switch the users around so that the user always corresponds with their time
var userTemp = userArray[i];
userArray[i] = userArray[j];
userArray[j] = userTemp;
//The loop will continue if even a single switch is made
keepSorting = true;
}
}
}
if(!keepSorting){
stopSort = true;
}
}
//String building starts here
var placeArray = [':first_place: **1st', ':second_place: **2nd', ':third_place: **3rd', '**4th', '**5th', '**6th', '**7th', '**8th', '**9th', '**10th'];
for(i = 0; i < totalObj; i++){
string = await string.concat(placeArray[i] + ": " + userArray[i] + "** - " + timeArray[i] + " \n\n");
console.log('butt');
}
console.log("This String:" + string);
return string;
}
I think problem is you are trying to await function with callback, it will not work => access to data.time may run before data = result[value]. If you need await callback, you can use custom Promise (or use util.promisify, more info here)
Promise:
function findStats(options) {
return new Promise((resolve, reject) => {
return stats.find(options, function (err, result) {
if (err) {
return reject(err)
}
return resolve(result)
})
})
}
utils.promisify
const util = require('util');
const findStats = util.promisify(stats.find);
Now you can use await in your function
async function getData(field, value) {
try {
const result = await findStats({})
const data = result.value
if (field === 'user') {
return data.user
}
if (field === 'time') {
return data.time
}
return 0
} catch (error) {
// here process error the way you like
// or remove try-catch block and sanitize error in your wrap function
}
}
I have a server that is written in Java running Rhino for js,
and I decide to rewrite this server into nodejs...
And I need to get data from DB synchronous like :
function executeRowsetParam(sql, p){
return DB.raw(sql,p)// returns object
}
so I can use it like :
var userName = executeRowsetParam('SELECT user_name FROM users where user_id = ?', ["123"]);
if(userName.getRow(0).getValue("user_name ") == "admin"){
//do sth
}
is just an a simple example sometimes I need to select from database data that i have to use in like 1000 lines of code so code like :
executeRowsetParam('SELECT user_name FROM users where user_id = ?', ["123"]).then((r)=>{
if(r.getRow(0).getValue("user_name ") == "admin"){
//do sth
}
})
won't work so well ...
I have code like this too:
IfExists(SQL,p){
if(DB.raw("selecect top 1 1 from" + sql,p) == 1){
return true
}else{return false}
and rewriting it into :
DB.raw("selecect top 1 1 from" + sql,p).then((r)=>{
if(r == 1){//do sth}else{//do else}
})
won't work for me
so is there some npm package that I can use to make selects from db synchronuch that would make my day.
is code like this would be okey ?
var Start = async () => {
var Server = require("./core/server/Server");
console.log('hi!');
console.dir("there is nothing to look at at the momment");
var db = require("./core/db/DB");
global.DB = new db()
function foo() {
return DB.executeSQL("asdasd", [123, 123])
}
console.dir(await foo());
console.dir('asd');
global.DEBUG = true;
global.NEW_GUID = require('uuid/v4');
var server = new Server()
server.start();
}
Start();
is that enough to let me use await in every single instance inside the server or have I make every single function async if I would use await?
Found and tested deasync works great!
var deasync = require('deasync');
module.exports = function DB(){
var knex = require('knex')(require("./../../config").DB);
this.executeRowsetParam = (sql, param) => {
let done = false;
knex.raw(sql, param).then((r => {
done = r;
})).catch((e => {
throw "Error in query "+ e
}))
deasync.loopWhile(function () { return !done; });
return done
}
}
I'm trying to generate a hashchain using the following code:
var async = require('async');
var _ = require('lodash');
var offset = 1e7;
var games = 1e7;
var game = games;
var serverSeed = '238asd1231hdsad123nds7a182312nbds1';
function loop(cb) {
var parallel = Math.min(game, 1000);
var inserts = _.range(parallel).map(function() {
return function(cb) {
serverSeed = genGameHash(serverSeed);
game--;
query('INSERT INTO `hash` SET `hash` = ' + pool.escape(serverSeed));
};
});
async.parallel(inserts, function(err) {
if (err) throw err;
// Clear the current line and move to the beginning.
var pct = 100 * (games - game) / games;
console.log('PROGRESS: ' + pct.toFixed(2) + '%')
if (game > 0){
loop(cb);
}else {
console.log('Done');
cb();
}
});
}
loop(function() {
console.log('Finished with SEED: ', serverSeed);
});
When I run this code it generates a hash chain of 1k hash's, while I'm trying to generate a chain of 1m hash's. It seems like async isn't working properly, but I have no idea why, there are no errors in console, nothing that points out a flaw.
Any ideas?
Do you can run it with smaller games (about 3000)?
Your parallel function nerver send done signal because the callback of inserts item never trigged. I think query function has two pramasters query(sql: string, callback?: (err, result) => void) (Typescript style).
I suggest you change your logic and flow like below block code:
var inserts = _.range(parallel).map(function() {
return function(cb) {
serverSeed = genGameHash(serverSeed);
query('INSERT INTO `hash` SET `hash` = ' + pool.escape(serverSeed), function(err, result) {
if(result && !err) {
game--;
}
cb(); // remember call the callback
});
};
});
In your code, you have used async.parallel, I think it is not good idea, too many connection has be open(1m). Recommeded for this case is parallelLimit
I'm trying to make it possible to choose different SSID's to switch the Wlan you are connected to from Browser.
var sys = require('sys');
var exec = require('child_process').exec;
app.get(prefix + '/wlan', function(req, res){
child = exec("iwlist wlan0 scan | grep ESSID", function(error, stdout, stderr){
if(error !== null){
console.log('Exec error ' + error);
}
else {
res.send(stdout);
}
});
});
This is my code so far to get a SSID list..
The Output is like that:
ESSID:"WLAN-GUEST" ESSID:"WLAN1" ESSID:"WLAN-GUEST" ESSID:"WLAN1" ESSID:"WLAN2"
I have no idea why two ESSID's are listed twice but my main question is, how can I parse this to JSON or how can I access each entry like an array (wlanlist[0])?
Edit:
I tried to stdout.replace(" ",", "); and JSON.parse but as it's async it's sent without changes. (Not sure if that would work as sync)
Edit2: Trying to access the data like that:
$(document).ready(function() {
$.get(prefix + '/wlan', function(wlanlist){
document.getElementById("wlanoptions").options[0] = new Option("Select your WLAN:","");
document.getElementById("wlanoptions").options[1] = new Option(wlanlist[0],wlanlist[0])
});
});
Final Result:
var wlanlistarray = stdout.split("ESSID:");
res.send(wlanlistarray);
In addition:
//extract ssid and remove quotes
var wlanlist = new Array;
var step1 = stdout.split("ESSID:");
for(i = 1; i < step1.length; i++){
var arr = new Array;
arr = step1[i].split('"');
//if exists in array -> continue; else create new entry in wlanlist
if(wlanlist.indexOf(arr[1]) === -1){wlanlist.push(arr[1]);}
else{continue;}
}
res.send(wlanlist);
This should return an array of SSIDs:
stdout.split("ESSID:")
Now clean up the " and you are all done.
I m creating mobile web application using html5 and javascript.I m having two javascript files. AttributesDatabase.js and AttributeView.js.From AttributeView.js i m calling one function from AttributeDatabase.js in that i m executing one select query.Now the query result should go to AtttributeView.js.But the Websql transaction is asynchronous call that is what it is not returning proper result.Is there any way to handle the websql result.
Please help if any way there?
Edited
AttributeView.js
var AttributeDAOObj = new AttributeDAO();
AttributeDAOObj.GetAttributeList();
alert(AttributeDAOObj.GetAttributeList()); //This alert is coming as undefined.
AttributeDAO.js
this.GetAttributeList = function () {
var baseDAOObj = new BaseDAO();
var query = "SELECT AttributeName FROM LOGS";
// this.Successcalbackfromsrc = this.myInstance.Successcalback;
var parm = { 'query': query, 'Successcalback': this.myInstance.Successcalback };
baseDAOObj.executeSql(parm);
}
//To Create database and execute sql queries.
function BaseDAO() {
this.myInstance = this;
//Creating database
this.GetMobileWebDB = function () {
if (dbName == null) {
var dbName = 'ABC';
}
var objMobileWebDB = window.openDatabase(dbName, "1.0", dbName, 5 * 1024 * 1024);
return objMobileWebDB;
}
//Executing queries and getting result
this.executeSql = function (query) {
var objMobileWebDB = this.myInstance.GetMobileWebDB();
objMobileWebDB.transaction(function (transaction) {
//In this transaction i m returning the result.The result value is coming.
transaction.executeSql(query, [], function (transaction, result) { return result; }, this.Errorclback);
});
}
}
The problem is in you succes call back (like in the comment to your question, stated by DCoder)
function (transaction, result) { return result; }
this is returning where to?
So this is how to do it (or at least one way)
you can do for example:
function (transaction,result){
console.log("yes, I have some result, but this doesn't say anything, empty result gives also a result");
// so check if there is a result:
if (result != null && result.rows != null) {
if (result.rows.length == 0) {
// do something if there is no result
}else{
for ( var i = 0; i < result.rows.length; i++) {
var row = result.rows.item(i);
var id = result.rows.item(i).id; //supposing there is an id in your result
console.log('Yeah! row id = '+id);
}
}
}else{
// do something if there is no result
}
};
note the code above can be compacter, but this is how to understand it better.
another way is to put this function is a seperate piece of code, so you keep the sql statement more compact and readable. Like you call you error callback this can be in your function (with this. in front of it) or a completely seperate function.