Unexpected Error with invoking getChildIndex in Google Docs AppScript on Body (Parent Element Type: BODY_SECTION)

I’m working on a Google Apps Script to replace placeholders in a Google Docs template.

Unexpected error while getting the method or property getChildIndex on object DocumentApp.Body.

Here is the relevant code section:

const elementType = element.getType();
/** @type {GoogleAppsScript.Document.Text} */
let textElement;

if (elementType === DocumentApp.ElementType.PARAGRAPH ||
    elementType === DocumentApp.ElementType.TABLE_CELL ||
    elementType === DocumentApp.ElementType.LIST_ITEM ||
    elementType === DocumentApp.ElementType.TEXT) {
    textElement = element.editAsText();
}

if (textElement) {

    const parent1 = textElement.getParent();
    const parentType = parent1.getType();

    try {
        let index = parent1.getChildIndex(textElement);
            
        //further code below, but crashing at getChildindex in the line aboveā€¦

    } catch (error) {
        console.error(`Error processing placeholder: ${error.message}`);
        throw error;
    }
}

My Understanding:

Any help would be appreciated!