Why won't my container component receive result of Async Await properly?
By : Cristian Omar Cruz
Date : March 29 2020, 07:55 AM
I wish did fix the issue. to @FelixKling and @Think-Twice . I did actually end up a) wrapping the util function in a Promise, and reading up on async await. Thought i understood that but there were some quirks i didnt. code :
export function checkLoginStatus() {
return new Promise((resolve, reject) => {
const unsubscribe = firebase.auth().onAuthStateChanged(user => {
unsubscribe(); // <--- calls itself and resolves with a successful user object or null
resolve(user);
}, reject('api has failed'));
});
}
isUserAlreadyAuthenticated = async () => {
try {
let user = await this.props.checkLoginStatus();
console.log('react side: ', user);
if (user && user.email) {
this.setState({
isLoggedIn: true, // <-- when true page navs to Homepage
});
}
} catch(err) {
console.log('catch | api error: ', err);
}
}
|
Async\Await with promise not working properly
By : Daniel Green
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I have an Async function and I send to another Async function that wait for the promise to resolve, but for some reason its not waiting for the reslove. , In this line: code :
connection.connect((err)=>{res(err)});
function makeQuery(connection, query) {
return new Promise((resolve, reject) => {
connection.connect(error => {
if(error) return reject(error);
connection.query(query, (error, results, fields) => {
if(error) return reject(error);
console.log(results);
resolve(results);
connection.close(); // actually you should open the connection to the db once ...
});
});
});
}
const openTemplate = header =>
makeQuery(connection, `SELECT * FROM streamingpages WHERE ID=${header.routerParamas.id}`); // MYSQL INJECTION POSSIBLE!!!!
Router.get("/openPage/:id",async(res,req)=>{
var params = res.getHeaderParams();
const result = await openTemplate(parms);
res.returnJson(result, "Success");
});
|
why my async await is not working properly
By : michade
Date : March 29 2020, 07:55 AM
I wish this help you When you use await when invoking a function, you're basically waiting for a promise inside that function to resolve. The invoked function is intended to return that promise. In your code, the login function does invoke connection.query, but there isn't any promise that waits for the query to resolve. code :
const login = async function () {
try {
console.log('Login process started');
const newToken = await jwt.sign({login:'login'},config['token-secret'],{ expiresIn: config['token-expires']});
let username = 'root_admin'
let password = 'Admin123';
let token = String(cryptojs.lib.WordArray.random(20));
console.log("token : "+token);
return new Promise(function(resolve, reject){
connection.query('SELECT * FROM users where username = ? ',[username], async function(err, rows) { // await won't do anything here,
// you should only use await with functions that return promises.
if (err) {
console.log("Looged out failed");
throw new Error(err);
} else {
const user = rows[0];
console.log("psdsdsd");
if(bcrypt.compareSync(password,user.passwordHash)) {
await connection.query('SELECT * FROM organizations where id = ? ',[user.organizationId], async function(err, org_data) {
if (err) {
console.log("Looged out failed");
throw new Error(err);
} else {
console.log("sdsd");
//console.log(org_data);
if(typeof org_data.name!='undefined') {
organization = org_data.name;
} else {
organization = 'VeriCurious';
}
//console.log(organization);
// create a token
const token = await jwt.sign({ id: user.id, username: user.username, organizationId: user.organizationId, organization: organization}, config['token-secret'], {
expiresIn: config['token-expires'] // expires in 30 minutes
});
console.log("Successfull loggedin");
console.log("New generated token : "+token);
resolve(token); // this signals the Promise to resolve.
// return token;
}
});
}
}
});
});
} catch (error) {
console.log(error);
}
}
const login = async function () {
// skipped a lot of code and logic since this is an example
const rows = await connection.query('SELECT * FROM users where username = ? ',[username]);
const user = rows[0];
const org_data = await connection.query('SELECT * FROM organizations where id = ? ',[user.organizationId]);
const organization = org_data.name;
return await jwt.sign({ id: user.id, username: user.username, organizationId: user.organizationId, organization: organization}, config['token-secret'], {
expiresIn: config['token-expires'] // expires in 30 minutes
});
}
|
Async/await function isn't working properly
By : user3270607
Date : March 29 2020, 07:55 AM
I wish this help you Try await Promise.all(files.map(async _ => {...})), but keep in mind .all will reject on first failure.
|
how to use async await on javascript frontend to receive data from async await node.js backend
By : user3719787
Date : March 29 2020, 07:55 AM
|