简体   繁体   中英

how to get a user by id with a get request to express using react native?

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文简体   中文繁体   中英对照 版本,有任何建议请联系yoyou2525@163.com。

I am trying to render One users info for the profile page in react native using express and axios.

This is the controller responsible for getting a user.

```// GET ONE USER BY ID
module.exports.findOneSingleUser = (req, res) => {
User.findOne({ _id: req.params.id })
    .then(oneSingleUser => res.json({ user: oneSingleUser }))
    .catch(err => res.json({ message: 'Something went wrong', error: err }));
}```

this is the code im using to make the axios request to the server and I am able to get all the users in the DB but I want to be able to render one user by id or the token that is stored for login and for login to persist which is working.

```
const ProfileInfo = (props) => {
const { navigation, route } = props
const authCtx = useContext(AuthContext);
const token= authCtx.token

const [userInfo, setUserInfo] = useState([]);


useEffect(() => {
    axios.get(`http://localhost:3000/api/users/`)
        .then(res => console.log(res.data))
        .catch(err => console.log(err))
}, [])```

this is the code in util folder that gets the token from the backend

import { useNavigation } from '@react-navigation/native';
const BASE_URL = 'http://localhost:3000'


    // ! LOGIN FUNCTION
    export async function authenticate(email,password ){
    const token = await axios.post(BASE_URL + '/api/login',
    {
        email: email,
        password: password,
    },{ withCredentials: true }
    );
    return token;
    }
    // ! REGISTER NEW USER FUNCTION
    export async function createUser(email, password) {
    const token = await axios.post(BASE_URL + '/api/register',
        {
            email: email,
            password: password,
        },{ withCredentials: true }
    );
    return token;

    }```


this is the screen where I have the profile info componet being used

    ```import React,{useEffect} from 'react'
    import ProfileInfo from '../components/Profile/ProfileInfo';
    import Statistics from '../components/Profile/Statistics';


    const ProfileScreen = (props) => {
    const {navigation} = props
    
    
    return (
        <>
            <ProfileInfo navigation={navigation}/>
            <Statistics />
        </>
    )
    }

    export default ProfileScreen```

    

How do or What do I need to pass into the url of the axios request to get the data for the user that is logged in?  thanks in advance.

when I change the server side to
    User.findOne({ token: req.params.token})

      &

    ``` useEffect(() => {
        axios.get(`http://localhost:3000/api/users/${token}`)
            .then(res => console.log(res.data))
            .catch(err => console.log(err))
    }, [])```


I get a user but it is only the first user in DB not the user that is logged in... not sure how to get the one user that is logged in.

No answers.You can refer to the related questions on the right.
暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM