简体   繁体   English

如何通过 id 获取用户并通过 get 请求使用 React Native 来表达?

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

I am trying to render One users info for the profile page in react native using express and axios.我正在尝试使用 express 和 axios 在本机反应中呈现个人资料页面的一个用户信息。

This is the controller responsible for getting a user.这是负责获取用户的 controller。

```// 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.这是我用来向服务器发出 axios 请求的代码,我能够获取数据库中的所有用户,但我希望能够通过 id 或存储用于登录和登录的令牌呈现一个用户坚持这是有效的。

```
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这是从后端获取令牌的 util 文件夹中的代码

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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为 React Native 应用程序的每个用户获取唯一 ID - How to get a unique ID for each user of React Native app 如何在本机反应中从firebase获取用户ID? - How to get user id from firebase in react native? 使用 React native expo 和 express 服务器获取请求权限错误 - GET request permissions error with React native expo and express server 对React Native应用程序中的动态SELECT查询的mySql / Express GET请求 - mySql/Express GET request to a dynamic SELECT query in a React Native application Express Server 无法从 React Native 应用程序获取请求 - Express Server cannot GET request from the React Native app 如何使用React Native Expo获得IOS和Android的捆绑包ID - How to get bundle ID for IOS and Android using react native expo 如何通过在 React Native 中使用国家/地区 id 获取状态数据 - how to get data of state by using country id in React Native 如何在 React Native 中使用一个信号获取玩家 ID? - How to get player ID using one signal in React Native? 快速端口3000,本地端口8081,如何发出请求 - port 3000 for express, port 8081 for react native, how to make get request Firestore - 如何在本机反应中通过 id 获取文档 - Firestore - how to get document by id in react native
 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM