apolloError:无法在客户端读取未定义的属性(读取'firstName') 问题的描述 我正在尝试为Facebook克隆实施用户统计。我正在使用阿波罗服务器和阿波罗客户端。该突变正在用于后端,并成功地创造了...

问题描述 投票:0回答:0

type定义


input AddUserInput { firstName: String! lastName: String! email: String! password: String! confirmPassword: String! pronoun: String genderIdentity: String! birthYear: Int! birthMonth: Int! birthDay: Int! } type Mutation { addUser(addUserInput: AddUserInput): Auth! login(email: String!, password: String!): Auth! }

resolver

module.exports = { Mutation: { // User registration async addUser( _, { addUserInput: { firstName, lastName, email, password, confirmPassword, pronoun, genderIdentity, birthYear, birthMonth, birthDay, }, } ) { // Validate user data const { errors, valid } = validateAddUserInput( firstName, lastName, email, password, confirmPassword, pronoun, genderIdentity, birthYear, birthMonth, birthDay ); if (!valid) { console.log('bonk on user registration'); throw new UserInputError('Errors', { errors }); } // Make sure user doesn't already exist const user = await User.findOne({ email }); if (user) { throw new UserInputError('Email address is taken', { errors: { email: 'This email address is taken', }, }); } // hash the password and create an auth token const hashedPassword = await bcrypt.hash(password, 12); // save birthday as ISO 8601 string const birthday = dayjs(`${birthYear}-${birthMonth}-${birthDay}`).toISOString(); const newUser = new User({ createdAt: new Date().toISOString(), firstName, lastName, email, password: hashedPassword, gender: { pronoun, genderIdentity, }, birthday, }); const res = await newUser.save(); const { _id } = res; const token = signToken({ email, firstName, lastName, _id, }); return { user: res, id: _id, token, }; }, }, }

准代码

client突变

import { gql } from '@apollo/client'; export const REGISTER_USER = gql` mutation AddUser($registerInput: RegisterInput) { addUser(registerInput: $registerInput) { token user { _id firstName lastName } } } `;

签名组件

仅显示对Adduser突变的代码

// Mutation const [addUser, { data, loading, error }] = useMutation(REGISTER_USER); // Registration form submit function const handleRegistrationFormSubmit = async (event) => { event.preventDefault(); // Make a copy of the registration form inputs const registrationFormInputs = { ...registrationForm }; // Format gender inputs for the API if (registrationForm.customGender === '') { delete registrationFormInputs.customGender; } else { registrationFormInputs.genderIdentity = registrationFormInputs.customGender; delete registrationFormInputs.customGender; } try { const { data } = await addUser({ variables: { ...registrationFormInputs }, }); // Get login token from response const registrationToken = data.register.token; Auth.login(registrationToken); // Clear the register form setRegistrationForm({ firstName: '', lastName: '', emailAddress: '', password: '', confirmPassword: '', birthDay: dayCurrent, birthMonth: monthCurrent, birthYear: yearCurrent, gender: '', pronoun: '', customGender: '', }); return <Navigate to="/" />; } catch (e) { ... } };
我尝试了什么

我尝试更改突变的名称并将每种单独类型添加到客户端突变中,但没有任何作用。


对其进行了探测

注册组件中有两个问题。 首先将所有电子邮件address属性重命名为电子邮件,因为该类型与财产不匹配。
    const [registrationForm, setRegistrationForm] = useState({
        ...
        email: '',
        ...
    });

第二,我没有正确输入adduser突变的变量。应该是

const { data } = await addUser({ variables: { addUserInput: { ...registrationFormInputs }, }, });

希望这对其他人有帮助。
    

reactjs graphql apollo registration
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.