Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Native - TypeError: Cannot read property 'prototype' of undefined #23

Open
Giuseppe-Lo-Re opened this issue Jul 11, 2024 · 0 comments

Comments

@Giuseppe-Lo-Re
Copy link

I'm working on an Android app with react native/android studio/@Azure service-bus. After this issue with @Azure service-bus Azure/azure-sdk-for-js#30342 , I installed fastestsmallesttextencoderdecoder:
I created globals.js:

 global.TextEncoder = require('fastestsmallesttextencoderdecoder').TextEncoder;
global.TextDecoder = require('fastestsmallesttextencoderdecoder').TextDecoder;

and imported in my form:

import '../../../../globals';
import 'react-native-get-random-values';
import { Buffer } from 'buffer';
global.Buffer = Buffer;
global.process = require('process');
import { WebSocketWrapper } from '../../../../wsWrapper';

import React, { useEffect, useState } from 'react';
import { View, Text, TouchableOpacity, Image } from 'react-native';
import { useForm } from 'react-hook-form';

import { ServiceBusClient } from '@azure/service-bus';
import CustomButton from '../../../../common/components/CustomButton/custom-button';
import CustomTextInput from '../../../../common/components/CustomButton/CustomTextInput/CustomTextInput';

import { styles } from './styles';

interface IForm {
    onClose: () => void; 
}

const connectionString = "*hidden*";
const topicName = "*hidden*";

const Form: React.FunctionComponent<IForm>= ({ onClose }) => {
    const [sending, setSending] = useState<boolean>(false) 
    const defaultValues = {
		firstName: '',
		lastName: '',
		description: '',
	};

	const { control, handleSubmit, formState: { errors }, reset } = useForm({
		defaultValues,
	});

    const sendMessage = async (data: any) => {
        setSending(true);

        const sbClient = new ServiceBusClient(connectionString, {
            webSocketOptions: {
                webSocket: WebSocketWrapper as any,
            },
        });
        const sender = sbClient.createSender(topicName);
        
        try {
            const message = {
                body: JSON.stringify(data),
                applicationProperties: { device: 1 },
            };
            console.log("Message to be sent:", message);
            console.log("Sending message...");
            await sender.sendMessages(message);
            console.log("Message sent successfully.");
            console.log(`Sent message: ${JSON.stringify(data)} to the topic: ${topicName}`);

            await sender.close();
        } catch (error) {
            console.log(error);
        } finally {
            await sbClient.close();
            setSending(false);
        }
    };

    const onSubmit = (data: any) => {
        console.log("data:", data);
        sendMessage(data).catch(error => {
            console.error("Submit error:", error);
        });
    };

    useEffect(() => {
		reset(defaultValues);
	}, []);

    return (
        <View style={styles.container}>
            <TouchableOpacity style={styles.closeButton} onPress={onClose}>
				<View style={styles.circle}>
					<Text style={styles.cross}>X</Text>
				</View>
			</TouchableOpacity>
            <View style={styles.logoContainer}>
                <Image
                    source={require('../../../../public/logo.png')}
                    style={styles.logo}
                />
            </View>
            <CustomTextInput
                control={control}
                name="firstName"
                rules={{ required: true }}
                error={errors.firstName}
                errorMessage="First name is required"
                placeholder="First name"
                style={styles.input}
            />
            <CustomTextInput
                control={control}
                name="lastName"
                rules={{ required: true }}
                error={errors.lastName}
                errorMessage="Last name is required"
                placeholder="Last name"
                style={styles.input}
            />
            <CustomTextInput
                control={control}
                name="description"
                rules={{ required: true }}
                error={errors.description}
                errorMessage="Description is required"
                placeholder="Description"
                multiline={true}
                numberOfLines={10}
                style={[styles.input, styles.textArea]}
            />
            <View style={styles.buttonWrapper}>
				<CustomButton 
                    title={sending ? "Sending..." : "Submit"}
                    disabled={sending}
                    buttonStyle={styles.button}
                    textStyle={styles.buttonText}
                    onPress={handleSubmit(onSubmit)} 
                />
			</View>
        </View>
    );
};

export default Form;

when i submit the form, I have this error:
Error 0: TypeError: Cannot read property 'prototype' of undefined

I hope you can help me solve

Regards,
Giuseppe Lo Re

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant