[SOLVED] Moralis.user.attributes _ethAddress

I am getting the ethAddress data fine, but typescript doesn’t like me passing a string to it. What is the proper data type for EthereumAddresses in the user Attributes ?

I know a basic question ,but couldn’t find it in the docs.
thank you

what is the code that you are trying to use and gets you that error?

I.m using the following as a component . seems to work as far as I have gone, only it won’t accept the et1 on line 59 giving this error : “Argument of type ‘{ etadd: any; }’ is not assignable to parameter of type ‘string’.ts(2345)
let et1: {
etadd: any;
}”

********************** code *************************

import {
Box,
Button,
FormLabel,
FormControl,
Accordion,
AccordionItem,
AccordionButton,
AccordionPanel,
AccordionIcon,
Input,
NumberInput,
NumberInputField,
NumberInputStepper,
NumberIncrementStepper,
NumberDecrementStepper
} from “@chakra-ui/react”;
import { useForm } from “react-hook-form”;
import { useMoralis } from “react-moralis”;

export default function FormComponent() {

const { user, Moralis } = useMoralis();

const {
  isAuthenticated
} = useMoralis();

const saveNewUser = async (
  em1: string,
  em2:string,
  eth1:string,
  eth2:string,
  sentTs:number): Promise<any> => {
  const Cuser = Moralis.Object.extend("Cuser");
  const cuser = new Cuser();
  cuser.set('muser', Moralis.User.current())
  cuser.set('email1', em1)
  cuser.set('email2', em2)
  cuser.set('ethadd1', eth1)
  cuser.set('ethadd2', eth2)
  cuser.set('senttokens', sentTs)
  cuser.save()
  console.log("i ran"); 
// return data
}
const { register, handleSubmit } = useForm(); 
const onSubmit = (data: any) => {

  // get eth address of user 
  let etadd = user!.attributes._ethAddress.toString;
  let em1 = data.email;
  let em2 = data.email2;
  let et1 = {etadd};
  let et2 = data.ethadd2;
  let sentTs = data.numOfTokens;

 // now save all user info to moralis db 
 saveNewUser(em1, em2, et1, et2, sentTs);

}

return(
<Box>
{isAuthenticated ? (
  <>







Expand for Instructions





1 You need to have our tokens in your account.


2 Enter the requested Information, remember the account address is NOT the private key and always keep your private keys safe.


3 Wait time to receive bonus tokens, up to 24 hours.


4 You can do this to as many friends as you want.





Your email address Friend's email address Friends ethereum address # Tokens you want to send Send Friend ) : (

Welcome to BankX Token Giveaway!

)} ); }

you can read here on how to post code on forum: READ BEFORE POSTING - How to post code in the forum

I am sorry , it won’t happen again , is this right ?

import {
    Box,
    Button,
    FormLabel,
    FormControl,
    Accordion,
    AccordionItem,
    AccordionButton,
    AccordionPanel,
    AccordionIcon,
    Input,
    NumberInput,
    NumberInputField,
    NumberInputStepper,
    NumberIncrementStepper,
    NumberDecrementStepper
  } from "@chakra-ui/react";
  import { useForm } from "react-hook-form";
  import { useMoralis } from "react-moralis";
  
  export default function FormComponent() {

    const { user, Moralis } = useMoralis();

    const {
      isAuthenticated
    } = useMoralis();

    const saveNewUser = async (
      em1: string,
      em2:string,
      eth1:string,
      eth2:string,
      sentTs:number): Promise<any> => {
      const Cuser = Moralis.Object.extend("Cuser");
      const cuser = new Cuser();
      cuser.set('muser', Moralis.User.current())
      cuser.set('email1', em1)
      cuser.set('email2', em2)
      cuser.set('ethadd1', eth1)
      cuser.set('ethadd2', eth2)
      cuser.set('senttokens', sentTs)
      cuser.save()
      console.log("i ran"); 
    // return data
    }
    const { register, handleSubmit } = useForm(); 
    const onSubmit = (data: any) => {
    
      // get eth address of user 
      let etadd = user!.attributes._ethAddress.toString;
      let em1 = data.email;
      let em2 = data.email2;
      let et1 = {etadd};
      let et2 = data.ethadd2;
      let sentTs = data.numOfTokens;

     // now save all user info to moralis db 
     saveNewUser(em1, em2, et1, et2, sentTs);
 
    }

    return(
    <Box>
    {isAuthenticated ? (
      <>
   <Box bg="tomato" 
        w="70%" 
        ml={15} 
        mt={4} 
        p={4} 
        color="blue" 
        borderWidth="2px" 
        borderRadius="lg">
   <Accordion allowToggle>
      <AccordionItem>
          <h2>
          <AccordionButton
              color="darkgreen.500"
          >
                  <Box textAlign="center">
                  Expand for Instructions
                  </Box>
              <AccordionIcon />
          </AccordionButton>
          </h2>
              <AccordionPanel pb={4}
                  color="lightblue"
                  fontWeight="bold"
              >
              <p>1  You need to have our tokens in your account.</p>
              <p>2  Enter the requested Information, remember the account address is NOT the private key and always keep your private keys safe.</p>
              <p>3  Wait time to receive bonus tokens, up to 24 hours.</p>
              <p>4  You can do this to as many friends as you want. </p>
              </AccordionPanel>
      </AccordionItem>
      </Accordion>            
</Box>

<Box bg="tomato" 
        w="70%" 
        ml={15} 
        mt={4} 
        p={4} 
        color="blue" 
        text-align="center" 
        borderWidth="2px" 
        borderRadius="lg" 
        border-color="white">
<form onSubmit={handleSubmit(onSubmit)}>
<FormControl isRequired mt={2}>
<FormLabel
    color="white"
    paddingTop="1.2rem"
    paddingLeft="3.5rem"
    textAlign="left"
    htmlFor="email">Your email address</FormLabel>
<Input {...register("email")}
    color="white"
    bg="teal.800"
    width="100%"
    type="email" 
    id="email"
    fontWeight="bold"
    aria-describedby="email-helper-text" /> 
  </FormControl>
    <FormControl isRequired>
<FormLabel
    color="white"
    paddingTop="1.5rem"
    textAlign="left"
    paddingLeft="3.5rem"
    htmlFor="email2">Friend's email address</FormLabel>
  <Input  {...register("email2")}
    color="white"
    bg="teal.800"
    width="100%"
    type="email" 
    id="email2" 
    fontWeight="bold"
    aria-describedby="email-helper-text" /> 
  </FormControl>
  <FormControl isRequired mt={6}>
  <FormLabel
    color="white"
    textAlign="left"
    paddingLeft="3.5rem"
    fontWeight="bold"
    htmlFor="ethadd2">Friends ethereum address</FormLabel>
  <Input  {...register("ethadd2")}
    color="white"
    bg="teal.800"
    width="100%"
    id="ethadd2" 
    fontWeight="bold"
     /> 
    </FormControl>
    <FormControl isRequired mt={6}>
    <FormLabel
    color="white"
    textAlign="left"
    paddingLeft="3.5rem"
    fontWeight="bold"
    text-align="center" 
    htmlFor="numOfTokens"
    ># Tokens you want to send</FormLabel>
    <NumberInput max={50} min={3}
    color="white"
    width="40%"
    id="numOfTokens"
    paddingLeft=".5rem"
    fontWeight="bold"              
    >
    <NumberInputField {...register("numOfTokens")}
    bg="teal.800"
    fontWeight="bold"
    />
    <NumberInputStepper>
      <NumberIncrementStepper />
      <NumberDecrementStepper />
    </NumberInputStepper>
    </NumberInput>
    </FormControl>
    <Button
      mt={4}
      mb={3}
      colorScheme="teal"
      type="submit"
    >
    Send Friend
    </Button>
  </form>
  </Box>
</> ) : ( <p>Welcome to X Token Giveaway!</p> )}
  </Box>  
 );       
}

yes, this looks like normal code :slight_smile:

1 Like

what do you get if you use console.log(et1, et2) before this line that calls saveNewUser?
if you use any instead of string then it works fine?

I just had to JSON.stringify it since it was an object Object. It works beautifully now saves my own custom user type object. Here’s the working code in case someone wants to see it .

[ I think it helped a lot when you said my code looked normal , thanks. lol ]

import {
    Box,
    Button,
    FormLabel,
    FormControl,
    Accordion,
    AccordionItem,
    AccordionButton,
    AccordionPanel,
    AccordionIcon,
    Input,
    NumberInput,
    NumberInputField,
    NumberInputStepper,
    NumberIncrementStepper,
    NumberDecrementStepper
  } from "@chakra-ui/react";
  import { useForm } from "react-hook-form";
  import { useMoralis } from "react-moralis";
  
  export default function FormComponent() {

    const { user, Moralis } = useMoralis();

    const {
      isAuthenticated
    } = useMoralis();

    const saveNewUser = async (
      em1: string,
      em2:string,
      eth1:string,
      eth2:string,
      sentTs:number): Promise<any> => {
      const Cuser = Moralis.Object.extend("Cuser");
      const cuser = new Cuser();
      cuser.set('muser', Moralis.User.current())
      cuser.set('email1', em1)
      cuser.set('email2', em2)
      cuser.set('ethadd1', eth1)
      cuser.set('ethadd2', eth2)
      cuser.set('senttokens', sentTs)
      cuser.save()
      console.log("i ran"); 
    // return data
    }
    const { register, handleSubmit } = useForm(); 
    const onSubmit = (data: any) => {
    
      // get eth address of user 
      let etadd = user!.get('ethAddress');

      let em1 = data.email;
      let em2 = data.email2;
      let et1 = JSON.stringify(etadd);
      let et2 = data.ethadd2;
      let sentTs = data.numOfTokens;

     // now save all user info to moralis db 
     saveNewUser(em1, em2, et1, et2, sentTs);
 
    }

    return(