2017-10-01 7 views
1

내가 있고, 내가 아래와 같이 Axios의와 샘플 침전물 Vuejs이 발행 어떤 답 :vuejs, 삭제

updateItem: function() { 
    var obj = {'name': this.name, 'dob': this.dob, 'gender': this.gender} 
    // console.log(obj) 
    var strngObj = qs.stringify(obj) 
    this.axios.put('http://localhost:3000/crud/update/', this.update_id, 
    strngObj, { 
    headers: { 
     'Content-Type': 'application/x-www-form-urlencoded' 
    } 
    } 
    // emulateJSON: true // fix cannot post as form data and urlencoded 
).then((resp) => { 
    if (resp.data.log === 'update success !') { 
     this.items.push(resp.data.data) 
     alert(resp.data.log) 
    } 
    console.log(resp.data) // check full object in browser console log 
    }) 
}, 

이 코드는 아래의 서버 측에게 있습니다

app.put('/crud/update/:UPid', (rqs, rsp, next) => { 
    rqs.header("Access-Control-Allow-Origin", "*"); 
    rqs.header("Access-Control-Allow-Methods", "POST,PUT"); 

    var obj = { "_id" : mongodb.ObjectID(rqs.params.UPid) }; 
    var update = { $set : { name: rqs.body.name, dob: rqs.body.dob, gender: 
    rqs.body.gender} }; // $set is object 

    var cursor = db.collection('info').updateMany(obj,update, function(err, res) 
    { 
    if (err) throw err; 
    rsp.send({log: "update success !",data: obj}); 
    console.log(rqs.body); 
    // console.log(update); 
    //db.close(); 
}); 
}) 

내가 검사를하고 크롬에서 디버그. 작성 및 읽기는 잘하지만 업데이트 (PUT 방식) 작업을 위해, 삭제 (DELETE) 전혀 작동하지 및 항상 오류 보여 아이디어

204 No Content

감사를!

+0

데이터베이스에 문제가 있다고 생각합니다. 데이터베이스에 코드를 추가 할 수 있습니까? – imcvampire

답변

1
업데이트 작업을위한 코드가 제대로, 204 개 상태는 (vuejs) 프론트 엔드에 백엔드 (nodejs 코드)에서 응답을 반환해야 의미

당신이 vuejs에 nodejs API에서 JSON 객체를 반환 할 필요가 당신의 그

nodejs 코드처럼 의미 :

app.put('/crud/update/:UPid', (rqs, rsp, next) => { 
`rqs.header("Access-Control-Allow-Origin", "*"); 
rqs.header("Access-Control-Allow-Methods", "POST,PUT");` 

    `var obj = { "_id" : mongodb.ObjectID(rqs.params.UPid) }; 
    var update = { $set : { name: rqs.body.name, dob: rqs.body.dob, gender: ` 
    ` rqs.body.gender} }; // $set is object 
    var cursor = db.collection('info').updateMany(obj,update, function(err, res) 
    { 
    if (err) throw err; 
    //delete this line 
    // rsp.send({log: "update success !",data: obj}); 
    console.log(rqs.body); 
    //add this line to return json object 
rsp.json({ status: "success"}); 
}); 
})` 

하고 vuejs 업데이트 방법은 다음과 같습니다

` updateItem: function() { 
    var obj = {'name': this.name, 'dob': this.dob, 'gender': this.gender} 
    // console.log(obj) 
    var strngObj = qs.stringify(obj) 
    this.axios.put('http://localhost:3000/crud/update/', this.update_id, 
    strngObj, { 
    headers: { 
     'Content-Type': 'application/x-www-form-urlencoded' 
    } 
    } 
    // emulateJSON: true // fix cannot post as form data and urlencoded 
).then((resp) => { 
     // parse json object response 
    var status = JSON.parse(resp.data.status); 
    }).catch(e => { 
       this.errors.push(e); 
    }) 
},` 
,