whatsapp-web.js remoteAuth crash when restart

  Kiến thức lập trình

Here is how i setup my whatsapp-web.js

async function setWhatsapp2(uuid: string) {
    // Load the session data
    return mongoose.connect("mongodb+srv://[email protected]/?retryWrites=true&w=majority").then(() => {
        const store = new MongoStore({ mongoose: mongoose });
        const client = new Client({
            authStrategy: new RemoteAuth(

                {
                    store: store,
                    backupSyncIntervalMs: 300000,
                    clientId: uuid,
                    dataPath: `./session/${uuid}`,


                }),
        });

        client.initialize();
        console.log("NOT AUTHENTICATED")
        client.on('remote_session_saved', () => {
            console.log(
                'remote_session_saved'
            )
        })


        client.on('qr', (qr: any) => {
            qrcode.generate(qr, { small: true });
        });

        client.on('ready', () => {
            console.log('Client is ready!');
        });

        client.on('disconnected', msg => {
            console.log('Client was logged out', msg);
        });

        client.on('authenticated', (session) => {
            //https://discord.com/api/webhooks/1181072012303675393/Zv0LiDW9g9yNwDLKXXOVJ31rxVJyp3YZGo_227ujDMw8181khK10utupzoX1uyr5vljn
            console.log(
                'AUTHENTICATED',
            )
        });

        client.on('auth_failure', (session) => {
            console.log(
                'AUTHENTICATION FAILURE',
            )
        });

        client.initialize();


        return client;
    });

}

and here is how i use it

app.get('/:uuid', async (req: Request, res: Response) => {
  const uuid = req.params.uuid;
  const client = await setWhatsapp2(
    uuid
  );
  const _client = client;
  if (_client.info) {
    return res.json({
      status: true,
      message: "Whatsapp is ready"
    })
  }

  return res.json({
    status: false,
    message: "Whatsapp is not ready"
  })


});

For first time when i hit http://localhost:8989/blabla, i can use it normally (Send message).

But when i restart it (Stop my express then re-run it again), then i hit http://localhost:8989/blabla. I get this

HTTP Server running on port 8989
NOT AUTHENTICATED
[Error: ENOENT: no such file or directory, open 'RemoteAuth-blabla.zip'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: 'RemoteAuth-blabla.zip'
}
[nodemon] app crashed - waiting for file changes before starting...

well there is no problem when saving it to mongodb, i can see the record there. did i miss something?

LEAVE A COMMENT