【问题标题】:redirect to created posts with vue and mongo使用 vue 和 mongo 重定向到创建的帖子
【发布时间】:2019-05-14 20:46:51
【问题描述】:

如何将用户重定向到他创建的帖子?我正在使用 mongo、express 和 vue 我的POST 请求如下所示:

async addOrder () {
      if(this.name.length > 0 && this.address.length > 4 && this.city.length > 3) {
      await PostsService.addOrder(
        this.postData = {
        order: this.cart,
        name: this.name,
        address: this.address,
        city: this.city,
        zip: this.zip,
        country: this.countries,
        summary: this.summary,
        status: this.status,
      })
      this.$router.push({ name: 'EachOrder', params: { id: order.id} )
        } else {
          alert('Fill the form')
        }
      },

我知道,我需要以某种方式创建 ID,但在此发布请求之前我无权访问。有什么想法吗:D?

(对不起我的英语^^)

【问题讨论】:

  • PostsService.addOrder 应该返回ID?您可以在那里捕获ID 然后导航。
  • 不,它不能 :/ 但我知道该怎么做 :D 谢谢你的帮助。

标签: javascript node.js mongodb vue.js axios


【解决方案1】:
async addOrder () {
  if(this.name.length > 0 && this.address.length > 4 && this.city.length > 3) {
  await PostsService.addOrder(
    this.postData = {
    order: this.cart,
    name: this.name,
    address: this.address,
    city: this.city,
    zip: this.zip,
    country: this.countries,
    summary: this.summary,
    status: this.status,
    randomNum: this.getRandomNum()
  })
  setTimeout(() => {
    this.$router.push({name: 'EachOrder', params: { id: this._id} })  
  }, 3000);

    } else {
      alert('Fill the form')
    }
  },
  getRandomNum() {
   return this.randomNum = Math.random() * 30
  },
  async getId() {
    const response = await PostsService.getOrder()
    const mapdata = response.data.map(item => {
      if (item.randomNum === this.randomNum) {
      this._id =  item._id
      }
      console.log(this._id)
    })
  }

但也许有人知道如何让它更容易,因为它有很多代码

【讨论】:

  • 您可以通过在后端(Express)上创建一个帖子 ID 来取消整个“randomNum”业务。您将在后端返回的响应中包含帖子 ID。然后,在前端 (Vue) 上,您只需向 PostsService 发出 POST 请求,然后等待响应。当响应返回时,您提取帖子 ID 并使用它来重定向用户。
  • 谢谢 :D 现在好多了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 1970-01-01
  • 1970-01-01
  • 2017-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多