Issue with the implementation of a custom TextInput class

Hello, I'm trying to implement the following class in order to create a TextInputBuilder without having to use setPlaceholder, setCustomId, etc when creating a new object
interface TextInputProps {
placeholder?: string;
customId?: string;
label?: string;
style?: TextInputStyle;
required?: boolean;
minLength?: number;
maxLength?: number;
}

class TextInput {

private text: TextInputBuilder;

constructor(props: TextInputProps) {
this.text = new TextInputBuilder();
this.setParameters(props);
}

public get(): TextInputBuilder {
return this.text;
}

private setParameters(props: TextInputProps) {
const functionsObject = {
'placeholder': this.text.setPlaceholder,
'customId': this.text.setCustomId,
'label': this.text.setLabel,
'style': this.text.setStyle,
'required': this.text.setRequired,
'minLength': this.text.setMinLength,
'maxLength': this.text.setMaxLength,
};

for (const [key, value] of Object.entries(props)) {
if (key && value) {
console.log(`key: ${key}, value: ${value}`);
functionsObject[key](value);
}
}
}
}
interface TextInputProps {
placeholder?: string;
customId?: string;
label?: string;
style?: TextInputStyle;
required?: boolean;
minLength?: number;
maxLength?: number;
}

class TextInput {

private text: TextInputBuilder;

constructor(props: TextInputProps) {
this.text = new TextInputBuilder();
this.setParameters(props);
}

public get(): TextInputBuilder {
return this.text;
}

private setParameters(props: TextInputProps) {
const functionsObject = {
'placeholder': this.text.setPlaceholder,
'customId': this.text.setCustomId,
'label': this.text.setLabel,
'style': this.text.setStyle,
'required': this.text.setRequired,
'minLength': this.text.setMinLength,
'maxLength': this.text.setMaxLength,
};

for (const [key, value] of Object.entries(props)) {
if (key && value) {
console.log(`key: ${key}, value: ${value}`);
functionsObject[key](value);
}
}
}
}
However I'm receiving the following error:
key: placeholder, value: Product name
PROJECT_PATH/node_modules/@discordjs/builders/src/components/textInput/TextInput.ts:107
this.data.placeholder = placeholderValidator.parse(placeholder);
^
TypeError: Cannot set properties of undefined (setting 'placeholder')
key: placeholder, value: Product name
PROJECT_PATH/node_modules/@discordjs/builders/src/components/textInput/TextInput.ts:107
this.data.placeholder = placeholderValidator.parse(placeholder);
^
TypeError: Cannot set properties of undefined (setting 'placeholder')
Any idea?
2 Replies
d.js toolkit
d.js toolkit14mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Bubba
Bubba14mo ago
Okay, thoughtless mistake: the functionsObject should receive a function with a parameter and return the setter from the discordjs TextInputBuilder class
'placeholder': (param: string) => this.text.setPlaceholder(param)
'placeholder': (param: string) => this.text.setPlaceholder(param)
Want results from more Discord servers?
Add your server