1

I'm currently trying to grab a value from an electron-json-storage database and return it.

function getWithExpiry(key) {
    var grabbed = "";
    storage.has(key, function (error, hasKey){
        if (error) throw error;

        if (hasKey){
            storage.get(key, function (error, data){
                if (error) throw error  
                const item = JSON.parse(data)
                const now = new Date()

                if (now.getTime() > item.expiry) {
                    storage.remove(key, function (error) {
                        if (error) throw error
                        grabbed = null
                    })
                }else{
                    console.log(item.value)
                    grabbed = item.value
                }
            })
        }
    })
    console.log(grabbed)
    return grabbed;
}

I'm calling it with this:

setTimeout(async () => {
         var tokenpass = getWithExpiry('captcha')
         console.log(tokenpass)
         await page.evaluate((tokenpass) => {document.getElementById("g-recaptcha-response").innerText = `${tokenpass}`}, tokenpass)
         await page.click('input.button').then(console.log('Clicked'));

The console.logs return in a different order than called. First it returns "" from console.log(grabbed) in getWithExpiry(key), then "" from console.log(tokenpass), then console.log('Clicked'), then the expected value from console.log(item.value). I think the order of these console.logs might be the reason why getWithExpiry doesn't return item.value.

1 Answer 1

1

Puppeteer

put await page.waitFor(1000); because the element takes some time to update, when you changed innterText

 async function getWithExpiry(key) {
  return new Promise((res, rej) => {
    storage.has(key, function (error, hasKey) {
      if (error) rej(error);

      if (hasKey) {
        storage.get(key, function (error, data) {
          if (error) rej(error);
          const item = JSON.parse(data);
          const now = new Date();

          if (now.getTime() > item.expiry) {
            storage.remove(key, function (error) {
              if (error) rej(error);
              res(null);
            });
          } else {
            console.log(item.value);
            res(item.value); 
          }
        });
      }
    });
  });
}

setTimeout(async () => {
  var tokenpass = await getWithExpiry("captcha");
  console.log(tokenpass);
  await page.evaluate((tokenpass) => {
    document.getElementById("g-recaptcha-response").innerText = `${tokenpass}`;
  }, tokenpass);

  await page.waitFor(1000);
  await page.click("input.button").then(console.log("Clicked"));
});
Sign up to request clarification or add additional context in comments.

1 Comment

fixed order, but now console.log(tokenpass) is returning Promise {<resolved>: ""}. grabbed is still exiting the function as "" instead of item.value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.