안녕하세요. it직 준비중인 학생입니다.
fetch를 이용해 name, email, password 데이터를 json 파일에 저장하고 싶은데 id만 저장이 됩니다.
아래는 제가 입력한 코드입니다.
import React, { useState } from 'react';
import { View, TextInput, Button } from 'react-native';
import styled from 'styled-components/native';
const Container = styled.View`
flex:1;
justify-content:center;
align-items:center;
background-color:${({theme})=> theme.background};
`;
const Signup1 = ({navigation}) => {
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const handleSignup = () => {
const formData = new FormData();
formData.append('name', name);
formData.append('email', email);
formData.append('password', password);
fetch('http://localhost:3001/users', {
method: 'POST',
headers : {"Content_type":"application/json"},
body: JSON.stringify({
name:formData.name,
email:formData.email,
password:formData.password,
})
})
.then(response => response.json())
.then(data => {
// handle successful signup
console.log(data);
})
.catch(error => {
// handle error
console.error(error);
});
};
return (
<View>
<TextInput
placeholder="Name"
onChangeText={text => setName(text)}
value={name}
/>
<TextInput
placeholder="Email"
onChangeText={text => setEmail(text)}
value={email}
/>
<TextInput
placeholder="Password"
onChangeText={text => setPassword(text)}
value={password}
secureTextEntry={true}
/>
<Button
title="Signup"
onPress={handleSignup}
/>
</View>
);
};
export default Signup1;
위 코드대로 입력하면 json 파일에 name, email,password가 들어갈 거라고 예상했는데
라고 나옵니다ㅠㅠ 어디가 잘못된 걸까요…
id 2부터는 회원가입해서 데이터를 계속 넣었는데 id만 저장되고 name, email, password는 저장이 되지 않습니다.