Robie rejestrowanie uzytkownika za pomoca useMutation, server otrzymuje request , wsadza nowego uzytkownika do bazy i wysyla jego dane do mutatora, ale alert
w useRegister pokazuje data:{}
jak to poprawic ?
const mutator = useRegister();
const register = async (data: User) => {
mutator.mutate(data);
alert("mutate" + JSON.stringify(mutator)); //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ten komunikat
};
import { Register } from "../../../utils/auth";
const useRegister = () => {
const queryClient = useQueryClient();
const mutator = useMutation(async (user: User) => {
return (
await Register("register", user),
{
onSettled: async () => {
alert("ssss " + JSON.stringify(mutator));
},
}
);
});
return mutator;
};
export { useRegister };
const Register = async (url: string, data: User) => {
return (
await api.post<User>(BASE_URL + url, data),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
}
);
};
export { Register }
import axios from "axios";
export const api = {
get: <T>(url: string, params?: object) =>
axios.get<T>(url, {
...params,
}),
post: <T>(url: string, data: any) => axios.post<T>(url, data, {}),
patch: <T>(url: string, data: any) => axios.patch<T>(url, data, {}),
delete: <T>(url: string) => axios.delete<T>(url, {}),
};
funkcja realizujace rzadanie na serwerze wyglada tak
export const register = async (req, res) => {
try {
const {
firstName,
lastName,
email,
password,
picturePath,
friends,
location,
occupation,
} = req.body;
console.log("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
const salt = await bcrypt.genSalt();
const passwordHash = await bcrypt.hash(password, salt);
if (
usersarr.filter((t) => {
return t.email === email || (t.password === password && t);
}).length > 0
) {
res.status(404).json({ message: "already exists user" });
console.log(1111111);
} else {
usersarr.push({
_id: new Date().getTime(),
firstName,
lastName: "sssssss",
email: email,
password: passwordHash,
picturePath,
friends: [],
location: "New York",
occupation: "private",
viewedProfile: Math.floor(Math.random() * 10000),
impressions: Math.floor(Math.random() * 10000),
});
res.status(201).json(
usersarr.filter((t) => {
return t.email === email && t;
})[0]
);
console.log(
usersarr.filter((t) => {
return t.email === email && t;
})[0]
);
}
} catch (err) {
res.status(500).json({ error: err.message });
}
};