You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using a mongoDB database and mongoose to save a session. I pass the serialised data into my saveSession function and upload it to my database.
When I load the session by using ig.state.deserialize() no error is thrown, but an error is throws when I try to use any other function of the igApiCleint.
In my database the cookies variable was saved as a string. I thought the problem lied here, so I thought I could solve the problem by saving it as a JSON object.
When I add the line serialized.cookies = JSON.parse(serialized.cookies);
then the cookies are saved as a JSON object.
I thought this would fix the error, but unfortunately it did not. So now I don't really know where the error could lie.
export{};import{IgApiClient}from"instagram-private-api";constSession=require('../models/models').Session;/** * Loads the session to the database. * * @param par_username {string} - Username of the user to save session for. * @return {Promise<void>} - Empty promise. */asyncfunctionloadSession(par_username) : Promise<any>{returnSession.findOne({username: par_username}).then((result)=>{returnresult;}).catch((error)=>{returnerror;});}/** * Saves the session to the database. * * @param par_username {string} - Username of the user to save session for. * @param serialized {JSON} - Serialized session data. * @return {Promise<void>} - Empty promise. */functionsaveSession(par_username : string,serialized : JSON) : void{constquery={'username': par_username};constoptions={upsert: true,useFindAndModify : false,new: true,setDefaultsOnInsert: true};constchange={$set:{'session' : serialized}};returnSession.findOneAndUpdate(query,change,options,function(err,doc){if(err)thrownewError(err);});}/** * Gets IgApiClient from username and password. * If a certain username has a session, then this session is used to create the IgApiClient * * @param username {string} - Username of the user to get IgApiClient for. * @param password {string} - Password of the user to get IgApiClient for. * @return {Promise<IgApiClient>} - Promise of an IgApiClient */asyncfunctiongetIgApiClient(username : string,password : string) : Promise<IgApiClient>{//Create a new client to return back to the user.constig=newIgApiClient();awaitig.state.generateDevice(username);//The code that has to be executed after every request.//Saves the state and cookies to database.ig.request.end$.subscribe(async()=>{constserialized=awaitig.state.serialize();//Line I thought would fix the problem//serialized.cookies = JSON.parse(serialized.cookies);deleteserialized.constants;saveSession((awaitig.account.currentUser()).username,serialized);});constexistingSession=awaitloadSession(username);if(existingSession!=null){//The code to load a user from the session database if it existsawaitig.state.deserialize(existingSession.session).catch((error)=>{thrownewError(error);});returnig;}else{//The code to create a user through loginconsole.log("Creating session for: "+username);awaitig.account.login(username,password).catch((error)=>{thrownewError(error);});awaitig.simulate.postLoginFlow();returnig;}}exports.getIgApiClient=getIgApiClient;
The text was updated successfully, but these errors were encountered:
The serialized output has euDCEnabled set to null, it should be undefined | boolean. Your state sets undefined to null. When saving the state (saveSession), you can use JSON.stringify(serialize) to "clear" any undefined values.
Description
I am using a mongoDB database and mongoose to save a session. I pass the serialised data into my saveSession function and upload it to my database.
When I load the session by using
ig.state.deserialize()
no error is thrown, but an error is throws when I try to use any other function of the igApiCleint.In my database the cookies variable was saved as a string. I thought the problem lied here, so I thought I could solve the problem by saving it as a JSON object.
When I add the line
serialized.cookies = JSON.parse(serialized.cookies);
then the cookies are saved as a JSON object.
I thought this would fix the error, but unfortunately it did not. So now I don't really know where the error could lie.
Error
Code
The text was updated successfully, but these errors were encountered: