Setting parameter to remoteCommand if value is a Promise result

UI Components for JSF
Post Reply
peterremec
Posts: 21
Joined: 16 Jan 2015, 15:50

18 Nov 2022, 12:25

I encountered to an interesting issue. I'm trying to invoke a remote command with some parameters. One of those parameters is a result of a JS Promise and when I try to read that parameter in a backing bean, it's value is empty.

In a for loop I'm iterating through an array of File objects and I need to invoke remote command for every file with it's b64 value passed as parameter.

HTML:

Code: Select all

<p:remoteCommand name="convertToPdfa" actionListener="#{bean.convertFile}" />
Bean:

Code: Select all

public String convertFile() {
    String index = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("index"); // not empty
    String data = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("data"); // not empty
    String retry = Boolean.parseBoolean(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("retry")); // empty
    return "";
}
JS:

Code: Select all

convertAll: async function (noOfFiles) {
    for (let i = 0; i < noOfFiles; i++) {
        await sendFileDataAndConvert(i);
    }
},
sendFileDataAndConvert: async function (index) {
    let promise = new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(getFile(index));
        reader.onload = () => resolve(reader.result);
        reader.onerror = error => reject(error);
    });

    // wait until the promise returns us a value
    let result = await promise;

    // "Now it's done!"
    console.log(result); // result is not empty, file is converted
    convertToPdfa([{name: 'index', value: index}, {name: 'data', result}, {name: 'retry', value: false}]);
}
What am I doing wrong here? Since result variable in a sendFileDataAndConvert function is not empty, I assume that data parameter is set before promise is resolved? How can I invoke remote command that data parameter won't be empty?

Thank you for answers.
PrimeFaces 11 | WebSphere Application Server 9.0.5.6 | MyFaces 2.0.17

peterremec
Posts: 21
Joined: 16 Jan 2015, 15:50

18 Nov 2022, 13:50

Just realized it was a typo. Instead of

Code: Select all

convertToPdfa([{name: 'index', value: index}, {name: 'data', result}, {name: 'retry', value: false}]);
it should be

Code: Select all

convertToPdfa([{name: 'index', value: index}, {name: 'data', value: result}, {name: 'retry', value: false}]);

:oops: :oops: :oops:
PrimeFaces 11 | WebSphere Application Server 9.0.5.6 | MyFaces 2.0.17

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 48 guests