使用vue和mongo重定向到创建的帖子

时间:2018-12-13 15:31:17

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

如何将用户重定向到他创建的帖子?我正在使用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?

(对不起,我的英语^^)

1 个答案:

答案 0 :(得分:0)

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)
    })
  }

但是也许有人知道如何简化它,因为有很多代码