Answer the question
In order to leave comments, you need to log in
How to deal with callbacks in nodejs?
Guys save please please!!!! I am writing an application in electronjs. Accordingly, there is nodejs. The task is very simple, but because of the callbacks I can’t solve it in any way and I don’t understand what to do in this situation at all. This is my first nodejs project, so don't throw rags if I'm asking very simple things.
The task is this:
1) Create a variable
2) Get the value from the cookie and put it in this variable.
3) If the required cookie does not exist, then we get the value from the database
4) We enter the obtained value into a variable and write it to the cookie.
I wrote this code:
PS To work with the database I use sequelize and SQLite
PSS I understand that this code is not working. I've just put the code in the order I see it should run, imagining that it's running synchronously.
var active_account=false; //Создаем переменную с изначальным значением false;
session.defaultSession.cookies.get({url: 'http://localhost',name: 'active_account'}, (error, cookie) => {
cookie=cookie[0];
active_account=cookie.value; //Получаем значение куки и заносим в переменную
});
if(active_account==false){ //Если значение осталось false (то есть значение не занесли), то начинаем получать значение из бд.
Account.findOne().then(function(account){ //Обращаемся к БД и берем первое значение
active_account=account.email; //Заносим значение в переменую
session.defaultSession.cookies.set({url: 'http://localhost', name: 'active_account', value: active_account}, (error) => {}) //Заносим значение в куки
});
}
console.log(active_account); //PROFIT
Answer the question
In order to leave comments, you need to log in
async function yourModule () {
...
const account = await Account.findOne();
active_account=account.email;
session.defaultSession.cookies.set({url: 'http://localhost', name: 'active_account', value: active_account}, (error) => {})
...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question