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