如何传递查询字符串以获取带有访存API的请求

时间:2018-07-03 05:04:53

标签: vuejs2 fetch query-string vuex

因此,im在登录后调度一个动作,并使用getter来检索用于发起另一个获取请求的凭据信息。该请求将正确地发送到服务器,并且getter返回适当的数据,但是服务器上的req.query仅返回[object Object]。这是代码:

获取组件中的信息

  created () {
    this.$store.dispatch('user/setFriends', {email: this.userInfo.email})
  },
  computed: {
 ...mapGetters('user', {
   userInfo: 'user_info'
  })
}

操作:

async setFriends ({
commit
}, email) {
  try {
    let request = new Request(`http://localhost:3000/users?id=${encodeURIComponent(email)}`)
    await fetch(request)
    await (r => r.data)
    await (r => commit('setFriends', r))
  } catch (error) {
    console.error(error.r)
  }
}

路由处理程序

router.get('/', function (req, res, next) {
  console.log(req.query.id)
});

其他尝试提取请求

  var url = new URL('http://localhost:3000/users')
  var params = {
    id: email
  }
  url.search = new URLSearchParams(params)

  await fetch(url)

在咨询如何使用访存写入查询字符串时,我还阅读了此链接Setting query string using Fetch GET request。任何帮助将不胜感激,谢谢

1 个答案:

答案 0 :(得分:0)

这里的问题是,分派动作时的有效负载不需要作为对象传递,而不必传递

created () {
this.$store.dispatch('user/setFriends', {email: this.userInfo.email})
},

简单地显示值

created () {
this.$store.dispatch('user/setFriends', this.userInfo.email)
},