[Vuejs]-AsyncData get profile from db

0👍

You may need to modify your route to the following, if you want to pass the id as parameter

router.get('/profile/:id', async (req, res) => {
  const { userId } = req.params.id;

  try {
    const profileUser = await User.findById(userId)

    res.send(profileUser)
  } catch (e) {
    console.log(e)
    res.status(400).json(e.message)
  }
})

and add profile id as route parameter

async asyncData({ $axios, store }) {
    try {
      let profile = await $axios.get('/profile/{profile_id_here}')
      
      return { profile }
    } catch (error) {
      console.log(error.message)
    }
  }

Otherwise, if you want to get the id of the authenticated user (may be resolved from a Bearer token), it needs to be set to the request object in you authentication middleware.

In your authentication middleware,

const user = await _authService.validateFromToken(bearerToken);
 if (user) {
      req.user = user;
  }

then you can access authenticated user as,

router.get('/profile', async (req, res) => {
  const { userId } = req.user._id;

  try {
    const profileUser = await User.findById(userId)

    res.send(profileUser)
  } catch (e) {
    console.log(e)
    res.status(400).json(e.message)
  }
})

Leave a comment