javascript – Application failed. The specified argument was out of range, Parameter name: Index not defined

I have this custom action in sharepoint where the page will be redirected to another url but the components of the URL depend on a faithful value in its main folder, Application failed. The specified argument was out of range, Parameter name: Indefinite index keeps appearing every time I press the button for the first time, but then the code runs smoothly

Here is the part of my code where the error occurs.

                                                                                                function getParentFolder () {
console.log (& # 39; getParentFolder & # 39;)
var defer = $ .Dferfer ();
var urlParams = new URLSearchParams (window.location.search);
var myParam = urlParams.get (& # 39; RootFolder & # 39;);
// var context = SP.ClientContext.get_current ();
var web = context.get_web ();
var folder = web.getFolderByServerRelativeUrl (myParam);
context.load (folder);
context.executeQueryAsync (function (data) {
defer.resolve (folder);
// onFolderSucceeded (folder)
}, function (sender, args) {
defer.reject (sender, args);
})
return postpone promise ();
};

is going to postpone. reject every time I press the button for the first time