0

I am beginner and my goal is to fetch items from DB, drag and sort them, and then save that sorted list in my DB. I have managed to fetch and set up drag sort vuejs component and I get key pair values of sorted items, but I don't know hot to save it to db.

When I execute saveNewSequence, I get console log value from forEach and nothing else, but when I refresh my page I get error: request aborted. And my list is not updated in DB.... I believe I have to somehow connect key value with id... can you please help.

Vue component:

data() {
    return {
      ropes: [],

    }
  },

methods:{

     saveNewSequence() {
      this.ropes.forEach((rope, key)=>{
        console.log('Key' + key +' '+ rope._id)
      })

      let postData = {}
      postData.ropes = this.ropes.map(rope=>{
        return{
          title: rope.title,
          description:rope.description,
          image: rope.image,
          price: rope.price,
          cartQuantity: rope.cartQuantity

        }
      })
      axios.post('https://zokadb.herokuapp.com/api/ropes', postData)
          .then((res) => {
            console.log(res)

          }).catch(error => {
        console.log(error)
      })


    },

}

this is my mongoDB model:

const ropeSchema = new mongoose.Schema({
    title: {
        type: String,
        required: true,

    },

    description: {
        type: String,
        required: true,

    },
    image: {
        type: String,
        required: false,
    },


    price: {
        type: Number,
        required: true,

    },
    cartQuantity: {
        type: Number,
        required: true,
    },


    date: {
        type: Date,
        default: Date.now
    }
})
Josip Marić
  • 227
  • 4
  • 21
  • What exactly is the problem? Are you running into issues or errors? – Aviv Ben Shahar Nov 04 '21 at 20:17
  • Here is my Trello app, build in Vue.js using as well D&D see the github repo to look for the source code if you'd like ------------------------ trellex.herokuapp.com ---------------------- https://github.com/Avivbens/trellex-proj-sprint4-public – Aviv Ben Shahar Nov 04 '21 at 20:18
  • @AvivBenShahar, well when i execute saveNewSequence, i get console log value from forEach and nothing else, but when i refresh my page i get error: request aborted. and my list is not updated in DB.... – Josip Marić Nov 04 '21 at 20:31

1 Answers1

0

Try :

postData = this.ropes.map(rope=>{
...

instead of :

postData.ropes = this.ropes.map(rope=>{
...

if you want to send post request with postData:

axios.post('https://zokadb.herokuapp.com/api/ropes', postData)
...
Nikola Pavicevic
  • 21,952
  • 9
  • 25
  • 46