[Vuejs]-How can I access Vuex Store from Node.js?

0👍

You can’t use the store from the client in your express server from the simple reason that all the data used in client side of your application, including vuex store is saved in the browser, and you don’t have access to it in the server. This is not the correct way to achieve your goal.

If you want to use data from the client you need to send it to your server, so you could use it there. So if you need the twitterName specifically you can do something like this:

router.get('/tweets/:twitterName', (req, res) => {
  // const name = 'name123'
  const name = req.params.twitterName
  T.get(
    'search/tweets',
    { from: name, count: 5 },
    function (err, data, response) {
      if (err) {
        return res.status(400).json('Oops! Something went wrong.')
      }
      data.twitterName = '<new-name>'
      res.status(200).send(data)
    }
  )
})

And from the vuejs store:

actions: {
   async getTweetData({ state, commit }) {
       const res = await axios.get('<your-server-ip>/' + state.twitterName)
       commit('setTwitterName', res.data.twitterName)
   } 
},
mutations: {
   setTwitterName(state, newTwitterName) {
       state.twitterName = newTwitterName
   }
}

Leave a comment