Node: error converting from txt to docx

Using the following code to convert txt → docx
The saveAs request fails with the following message: Invalid model type a

I’m using the latest asposewordscloud


  let saveRequest = new SaveAsRequest({
        name: "translation.txt",
        saveOptionsData: new DocxSaveOptionsData(
            {
                fileName: "translation.docx",
                saveFormat: "docx"
            })
    });

    let saveResult = await wordsApi.saveAs(saveRequest)

Please share your input document for analysis, as it seems to be a document-related issue.

The text is generated - it is output from deepL translator.
We translate the text like this:

const result = await translator.translateText(text, null, targetLanguage as deepl.TargetLanguageCode);

Then we want to convert the result.text to docx.

I tried to save the text to file and then convert it separately and it works. No idea on what is wrong.

Here is the function we are using

export async function convertTextToDocument(text: string): Promise<Buffer> {

    console.log("Converting text to document");

    // upload file to the Aspose cloud
    const textStream = Readable.from(text);

    const uploadRequest = new UploadFileRequest();
    uploadRequest.path = "translation.txt";
    uploadRequest.fileContent = textStream;

    const uploadResult = await wordsApi.uploadFile(uploadRequest);

    if (uploadResult.response.statusCode !== 200) {
        console.error("Error uploading file: " +  uploadResult.response.statusMessage);
        throw new Error("Error uploading file");
    }

    let saveRequest = new SaveAsRequest({
        name: "translation.txt",
        saveOptionsData: new DocxSaveOptionsData(
            {
                fileName: "translation.docx",
                saveFormat: "docx"
            })
    });

    let saveResult = await wordsApi.saveAs(saveRequest)


    if (saveResult.response.statusCode !== 200) {
        console.error("Error saving file: " + saveResult.response.statusMessage);
        throw new Error("Error saving file");
    }

    const downloadRequest = new DownloadFileRequest({ path: "translation.docx" });
    const downloadResult = await wordsApi.downloadFile(downloadRequest);

    if (downloadResult.response.statusCode !== 200) {
        console.error("Error downloading file: " + downloadResult.response.statusMessage);
        throw new Error("Error downloading file");
    }

    return downloadResult.body;
}

Could you please try this method instead of saveas

if the error persists with the convert document method, I will need the input document.

I think ConvertDocumentRequest is for conversion from docx.
I’m attempting the opposite conversion, i.e. text → docx.

Anyway, the conversion fails only in my next.js app. If I extract the code and run it separately, it works. Trying to figure out, what is going out, but could not figure it out so far.

I’m taking that back.
Using ConvertDocumentRequest worked.

Glad to hear you were able to resolve the issue.