sqlite3 db asynchronous problem in function [duplicate] - javascript

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
I have a problem with function check_mod.
function check_mod(user_id) {
db.each(`SELECT mode FROM users WHERE id = ` + user_id, [], (err, row) => {
if (err) {
throw err;
}
if(row.mode == 1)
{
return true;
}
});
}
if(check_mod('286927644405137408')) console.log("okay");
When i try to use this function, it returns "undefined" and i don't know why. I think that i should use async and await but i don't know how to use it.

I'm not sure but, I do notice that there is no semicolon at the end of the sql statement which could be causing the issue.

Related

Can't set value inside of the function [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
I can't set value inside function. When I try to alert this value, I'm getting: undefined. How to take this value outside?
var startTrasyLat;
L.esri.Geocoding.geocode()
.text(document.getElementById('start').value)
.run(function (err, results, response) {
if (err) {
console.log(err);
return;
}
startTrasyLat = results['results'][0].latlng.lat;
});
alert(startTrasyLat);
Looks like you are trying to call an asynchronous function. You should put the alert inside said function, like this:
var startTrasyLat;
L.esri.Geocoding.geocode().text(document.getElementById('start').value).run(function (err, results, response) {
if (err) {
console.log(err);
return;
}
startTrasyLat = results['results'][0].latlng.lat;
alert(startTrasyLat);
});

How do I use a callback in a for loop? - javascript [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
I've got a problem with getting data from a callback inside a for loop
getClipsFromFollowers: function (gameList, streamerList, callback) {
var allClips = []
console.log("gamelist in getclips", gameList);
for (game of gameList) {
console.log("game in getclips", game);
clipsGameManager.getListByName(game, function (error, list) {
if (list == []) {
twitchManager.getTopClipsFromGame(game, function (clips) {
clipsGameManager.createGameClips(game, clips, function (error, status) {
if (error != null) {
console.log(error)
} else {
if(list != null){
console.log("listfromGame getclips", list)
allClips = allClips.concat(list)
}
}
})
allClips = allClips.concat(clips)
console.log(clips);
})
}
})
}
console.log("allclips in getclips", allClips);
callback(null, allClips)
}
The logs looks like this:
gamelist in getclips [ 'Overwatch' ]
game in getclips Overwatch
allclips in getclips []
[]
So my question is what can I do to make this work? I've tried to use await() with no success.
Thanks in advance!

NodeJS Function returning undefined [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
I have a function that needs to get data from another function, but the second one is returning undefined and i don't know why.
function firstOne() {
const companys = companysList();
console.log(companys); // LOG UNDEFINED
};
.
function companysList(){
db.query('SELECT id FROM companys WHERE id > 0', (error, result) =>{
if (error) throw error;
console.log(JSON.stringify(result)); // LOG THE ids!
return result;
});
};
your companysList function doesn't return anything

JS/MongoDB: How to get count() value of collection? [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I'm trying to connecto to a mongoDB and count for all documents of an article:
var MongoClient = require('mongodb').MongoClient
var mongoUrl = 'mongodb://localhost:27017/test'
MongoClient.connect(mongoUrl, function (err, db) {
if (!err) console.log('Connected successfully to server: ' + mongoUrl)
var articles = db.collection('articles')
console.log(articles.count())
db.close()
})
But I do get the output Promise { <pending> } instead of a number.
db.collection('articles').count()
this line return promise as pointed in documentation. You need to handle this promise properly to get article count (it's a asynchronous operation so you need to wait unknown time for query result)
you need to do something like this
db.collection('articles').count().then(function(result){
console.log(result)
}, function(err){
return console.log(err);
});

Node.js MongoDB collection.find().toArray returns nothing [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 5 years ago.
Although I found similar questions to mine, I couldn't solve the problem on my own.
In my '../models/user' model I want to find all users and put them into array, and return that array to the controller(where I will use the info).
Here is my code:
var mongoDatabase = require('../db');
var database = mongoDatabase.getDb();
function find() {
var test;
database.collection("customers").find().toArray( function(err, docs) {
if(err) throw err;
console.log(docs); //works fine
//I'd like to return docs array to the caller
test = docs;
});
console.log(test); //test is undefined
}
module.exports = {
find
};
I also noticed, that 'console.log(test)' goes before 'console.log(docs)'. I tried passing 'docs' argument as function parameter to 'find', but without result.
The best way is to use Promises. Do it like this.
function getUsers () {
return new Promise(function(resolve, reject) {
database.collection("customers").find().toArray( function(err, docs) {
if (err) {
// Reject the Promise with an error
return reject(err)
}
// Resolve (or fulfill) the promise with data
return resolve(docs)
})
})
}

Categories