Open cogor opened 3 weeks ago
Can you share how you're storing and loading cookies? I'm seeing this comment under the branch for that error in v5 of the library:
} else if (!(cookie instanceof Cookie)) {
// If you're seeing this error, and are passing in a Cookie object,
// it *might* be a Cookie object from another loaded version of tough-cookie.
const err = new Error(
'First argument to setCookie must be a Cookie object or string',
)
return options?.ignoreError
? promiseCallback.resolve(undefined)
: promiseCallback.reject(err)
}
I storing cookies in json file
// Saving
const cookies = this.scraper.getCookies();
const serializedCookies = JSON.stringify(cookies, null, 2);
writeFileSync(this.COOKIE_FILE, serializedCookies, 'utf8');
// Restoring
const fileContent = readFileSync(this.COOKIE_FILE, 'utf-8');
const cookies = JSON.parse(fileContent).reduce((acc: Cookie[], c) => {
const cookie = Cookie.fromJSON(c);
return cookie ? [...acc, cookie] : acc;
}, []);
this.scraper.setCookies(cookies);
I saving cookies via
getCookies()
function When I try to restore then, I getting an error"Error: First argument to setCookie must be a Cookie object or string"
I downgraded version oftough-cookie
to 4.1.2(was v5.0.0) and now it works well