'loading the signin before the home page with nodejs
I want to make a POST to website x which requires a login but the problem is that the page load before siginin(check the console.log) how to fix this please
var request = require("request");
var headers = {
authority: "",
origin: url,
"content-type": "application/x-www-form-urlencoded",
cookie: "",
};
var dataString = "";
var options = {
url: loginUrl,
method: "POST",
headers: headers,
body: dataString,
};
function callback(error, response, body) {
if (!error) {
console.log("login successfully");
}
}
async function ok() {
await request(options, callback);
let result = await request.get(websiteUrl);
const page = result.data;
console.log("page loading");
}
ok();
console.log:
page loading
login successfully
Solution 1:[1]
The request package is deprecated. It does not support promises/async/await. Use a package such as axios, which supports promises, hence supporting async/await.
var axios = require("axios")
...
async function ok() {
await axios(options, callback);
let result = await request.get(websiteUrl);
const page = result.data;
console.log("page loading");
}
You may have to change the format of options, as per docs, but it should be similar to above.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ionică Bizău |
