This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
How do I convert an existing callback API to promises?
(24 answers)
Closed 2 years ago.
I'm fairly new to JS, this being my first project in it (using sqlite and the twitter api). I've been stuck for a while now on this issue. I can get await and async to work fine in other test programs but when it comes to this one the await within the following function isn't waiting for getmin() to conclude before moving on, which is causing issues in my for loop.
In the same vein like I said this is my first js project, it's highly likely to be wildly unoptimised. If something stands out as inefficient to you let me know and I'll know for future what to do/avoid.
async function following(err, data, response) {
if (err) {
console.log(err)
} else {
for (let i = 0; i < data.users.length; i++){
let user = data.users[i]
userinfo.name = user.name
userinfo.screen_name = user.screen_name
userinfo.user_ID = user.id
maxparams.user_id = user.id
new_min_params.user_id = user.id
console.log(new_min_params)
x = await getmin()
console.log(x)
// T.get('statuses/user_timeline', maxparams, getmax)
// T.get('statuses/user_timeline', tweetparams, gettweets)
//databasedata = [data.users.screen_name, minTweetID, maxTweetID]
}
// console.log(response_dictionary)
// console.log("")
cursorscore = 0
}
function getmin() {
let sql = `SELECT MaxTweetID FROM following WHERE Handle = ?`;
db.get(sql, [userinfo.screen_name], (err, row) => {
if (err) {
return console.error(err.message);
} else {
return (row ? updatemin(userinfo.screen_name) : T.get('statuses/user_timeline', new_min_params, get_tenth_tweet))
}});
}
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
so I have a function which open a txt file, extracts some lines and returns an array.
Problem is, it doesn't..
I console.log'd the last line before the return which printed an array, so I'm confused.
Tried console.logging everything but for some reason it will ALWAYS RETURN UNDEFINED
function Collect_Games(Amount){
let Msg = "",
userkeys = [];
fs.readFile("./SETTINGS/games.txt", "utf8", (error, data) => {
if(error) {
return;
} else {
let keys = data.split("\n");
if(keys.length < Amount) {
dont = false;
Msg = "I don't have enough keys for you, please try again later..";
} else {
userkeys = keys.splice(0, Amount);
/*
for(var i=0 ; i < userkeys.length ; i++){
Msg+= userkeys[i]+"\r\n";
}*/
fs.writeFile("./SETTINGS/games.txt", keys.join("\n"), error => {
if(error) {
}
console.log(userkeys); // this actually prints an array..
return userkeys;
});
}
}
});
}
Expecting this: console.log(Collect_Games(3)); to return an array, however it returns undefined
write a return state before fs.readFile as below.
return fs.readFile("./SETTINGS/games.txt", "utf8", (error, data) => {
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I am trying to pass the array named 'sheetsArray' out of the code below so that I may utilize it for some tasks. I cannot for the life of me figure out how to do this, despite trying many things and googling for hours. I'm sure it's easy but I'm not even sure what I should be searching that I'm not.
var sheetsArrayOut = sheets.spreadsheets.get({
auth: googleauth,
spreadsheetId: outputDOCID,
}, function(err,response) {
if (err) {
console.log('ERROR:' + err);
return
}
var sheets = response.sheets;
if (sheets.length == 0) {
console.log('No data found.');
} else {
var sheetsArray = [];
for (i = 0; i < sheets.length; i++) {
sheetsArray.push(sheets[i].properties.title);
}
}
console.log(sheetsArray[4]); // this returns the sheet name
return sheetsArray;
});
console.log(sheetsArrayOut[4]); // this returns undefined
It returns undefined because I suspect the function is asynchronous. In this instance you can probably use a callback to good effect:
function getData(function (sheetsArray) {
// do things with sheetsArray
});
function getData(callback) {
sheets.spreadsheets.get({
auth: googleauth,
spreadsheetId: outputDOCID,
}, function(err,response) {
// do a bunch of things
callback(sheetsArray);
});
}
The first = in var = sheetsArrayOut = sheets.spreadsheets.get(...) is invalid syntax.
Use var sheetsArrayOut = sheets.spreadsheets.get(...) instead.
Why don't you just reassign the value at the end of the request
var sheetsArrayOut;
sheets.spreadsheets.get({
auth: googleauth,
spreadsheetId: outputDOCID,
}, function(err,response) {
//your functional code
sheetsArrayOut = sheetsArray;
});
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I am trying to implement nested api calls in ionic3
submitLoginForm() {
var username = this.formData.value.username;
var password = this.formData.value.password;
var bodyjson;
var userId;
this.ApidataService.logincheck(username,password).subscribe(data => {
this.logindetail = data;
bodyjson = JSON.parse(this.logindetail._body);
userId = bodyjson.user.id;
if( userId != undefined){
this.ApidataService.userdata(userId).subscribe(data => {
this.userInfo = data;
});
}
console.log(userId);
});
console.log(this.userInfo);
}
Now when I call this function the nested api returns undefined for the first call and then from the second call onwards it returns proper values however when I try to log the argument that is being sent to the api I notice that the correct value is passed each time. I believe that this is due to some synchronization issues but am unable to figure out the issue and the solution for the same.
You can use flatMap here.It seems you don't need to retrieve results of both requests hence this should work for you.
import { Observable } from "rxjs/Observable";
import 'rxjs/add/observable/flatMap';
Then:
this.ApidataService.logincheck(username,password).flatMap(
(data) => {
this.logindetail = data;
bodyjson = JSON.parse(this.logindetail._body);
userId = bodyjson.user.id;
if( userId != undefined){
return this.ApidataService.userdata(userId);
}else{
return Observable.empty();
}
}).map((res: Response) => res ? res.json() : {})
.subscribe(data => {
this.userInfo = data ? data : {};
});
You can read more about flatMap here
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
why wont usernametoid function return anything? i know it exist by consoling it out, but it wont store in the otherplayerid variable? why?
my app: ( calling post api kill)
var userFunc = require('../factory/user_factory.js');
app.post('/api/kill', function (req, res) {
var username = "signature";//req.query.username;
var otherplayerid = userFunc.usernametoid(username);
if (!(otherplayerid)) {
console.log("other player is acually " + otherplayerid);
result.push("denne brukeren finnes ikke! " + otherplayerid);
} else {
}
});
and my user_factory:
var articles = require('../controllers/articles.server.controller'),
path = require('path'),
mongoose = require('mongoose'),
Article = mongoose.model('Article'),
Users = mongoose.model('User'),
errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller'));
exports.usernametoid = usernametoid;
function usernametoid(id) {
var query = Users.findOne( { username : id } );
query.exec(function(err, datas) {
console.log(datas._id);
return datas._id;
});
}
console:
other player is acually undefined
57c1c0f3b6b20c011242bf22
You need to read about asynchronous calls. Which is db request. Simple fix is callback:
function something(data, callback) {
return callback('some data from db')
}
something('x', function(cb) {
console.log(cb)
}
it is good practise to return two values (error, callback). But you can read it later on.
There are also promises. You can read about them insted of callbacks, but it is recommended to know both.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
router.post('/loginv', function (req,res) {
var id = req.body.id;
var pass = req.body.pass;
if(login.login(id,pass)=='validated'){
res.sendfile('views/welcome.html');
}else{
res.send('dont give up');
}
var result = login.login(id,pass);
console.log(result);
});
module.exports={
login : function(id,pass){
var q = "SELECT * FROM user where id = ? and pass = ?";
var ret = 'default';
DB.DB.query(q, [id,pass], function (error,result) {
if(error){
console.log('not found');
ret = 'unrecognized';
} else{
console.log('found');
ret = 'validated';
}
});
return ret;
}};
console.log :
GET /login 304 4.028 ms - -
default
POST /loginv 200 40.558 ms - 12
found
found
as you can see the value ret returned from the following code is not being changed although it follows the procedure of the function properly..
i'm new to node js and js stuff so any comments and advice will definitely be helpful thx :)
DB.query() is async, so login function do not wait the execution of the code before returning ret. You need to add a callback to the login method (or use a promise).
A working code:
module.exports = {
login : function(id,pass,cb){
var q = "SELECT * FROM user where id = ? and pass = ?";
DB.DB.query(q, [id,pass], function (error,result) {
if(error){
console.log('not found');
cb(error, 'unrecognized');
} else{
console.log('found');
cb(null, 'validated');
}
});
}
};
The other file:
router.post('/loginv', function (req,res) {
var id = req.body.id;
var pass = req.body.pass;
login.login(id,pass, function(err, result) {
if (err) {
console.log(err);
res.send('dont give up');
return;
}
if (result === 'validated') {
res.sendfile('views/welcome.html');
}else{
console.log('Unknown error');
}
})
});
I suggest you to read links posted here and this question which can gives you an idea about callbacks and promises.
PS: I do not know which library you are using for DB, but you MUST sanitize input before doing queries, if the library does not do it for you