inconsistent behaviour of smartapi-javascript for the same input showing jwt-token error and sometime working fine.


  • for the PlaceOrder function below, if I pass the same input it gives a jwt-token error sometimes and sometimes it works fine.

    '
    let { SmartAPI } = require("smartapi-javascript");
    const totp = require("totp-generator");

    const PlaceOrder = async ({ apikey, user_id, totp_secret, password,tradingsymbol,Qty,transactiontype,symboltoken }) => {

    try {
        const token = totp(totp_secret);
        
        let smart_api = new SmartAPI({
            api_key: apikey,
        });
        let data = await smart_api
            .generateSession(user_id, password, token)
            .then(() => {
    
                return smart_api.placeOrder({
                    "variety": "NORMAL",
                    "tradingsymbol": tradingsymbol,
                    "symboltoken": symboltoken,
                    "transactiontype": transactiontype,
                    "exchange": "NFO",
                    "ordertype": "MARKET",
                    "producttype": "CARRYFORWARD",
                    "duration": "DAY",
                    "price": "0",
                    "squareoff": "0",
                    "stoploss": "0",
                    "quantity": Qty
                })
            })
            .catch((err) => {
                console.log("error generating" , err)
                return {}
            });
        return data;
    }
    catch (err) {
        console.log(err.message);
        return {}
    }
    

    }'

    I tried to to debug the error it shows problem in
    'node_modules\smartapi-javascript\lib\smartapi-connect.js:178:40'

    even generateSession method in node modules is getting every parameter right it throws error which is catch block
    'self.generateSession = function (client_code, password, totp) {
    let params = {
    clientcode: client_code,
    password: password,
    totp: totp,
    };
    let token_data = post_request('user_login', params);

    	token_data
    		.then((response) => {
    			if (response.status) {
    				// console.log(response.data.jwtToken)
    				self.setClientCode(client_code);
    
    				self.setAccessToken(response.data.jwtToken);
    				self.setPublicToken(response.data.refreshToken);
    			}
    		})
    		.catch(function (err) {
    			throw err;
    		});
    
    	return token_data;
    };
    

    '

    This breaks the code and I am trying to place an order for multiple brokers and multiple users but because of this, the whole code breaks.
    please give some solution for this.