DataTables Editor 인라인 필드에서 보낸 데이터를 기반으로 mongo db를 업데이트하는 Express 게시 경로가 있습니다. 지금까지 그렇게 좋았고 데이터베이스가 잘 업데이트되고 있습니다. 넘어지는 부분은 업데이트 쿼리가 성공적으로 실행 된 후 원래 페이지로 다시 리디렉션하려는 경우입니다. 노드 콘솔은 원래 페이지에 대한 GET 라우트가 호출되고 있음을 나타내지 만 페이지 자체는로드되지 않습니다. 직접 호출 할 때POST 후 노드/익스프레스 리디렉션
router.post('/edit', function(req,res){
var theKeys = _.keys(req.body);
var theMeat = theKeys[1];
var bits1 = theMeat.split("][");
// this is the updated value
var newVal = req.body[theMeat];
// this is the row _id
var cleanId = bits1[0].replace("data[row_","");
// this is the field to update
var cleanRow = bits1[1].replace("]","");
// cast the id string back to an ObjectId
var updId = new ObjectId(cleanId);
var query = {};
query[cleanRow] = newVal;
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/gts';
MongoClient.connect(url, function(err, db){
if(err){
console.log("Error connecting to the server", err);
}else{
//console.log("Connected for Edit");
var collection = db.collection('events');
collection.updateOne({"_id":updId},{$set:query},function(err,result){
if(err){
console.log("Error adding message", err);
}else if(result){
//console.log("Update attempted", result.result);
res.redirect('/admin');
}else{
console.log("Unknown error");
}
});
}
});
});
가져 오기 경로가 잘 작동하지만 이런 식으로 호출 할 때 중지 할 것 같다 다음과 같이
에서 POST 기능에 대한 코드입니다.
리디렉션 자체를 제외한 모든 것을 제거 할 때 동일한 문제가 발생하는 POST 경로에는 아무 것도 없습니다.
router.post('/test',function(req,res){
res.redirect('/admin');
});
도와주세요!
또한 입력 샘플을주십시오. '.split()'와'.replace()'로 당신이하는 일은 내게 유쾌하지 않습니다. – Tomalak
split과 replace는 DataTables 편집기가 요청으로 보내는 다중 레벨 JSON 객체의 전체 개 아침 식사에서 업데이트에 필요한 데이터를 가져 오는 것입니다. 우아하지는 않지만 실제적으로 업데이트가 작동합니다. 위에서 말한 것처럼 리디렉션이 작동하지 않는 경우에도 (편집 된 질문 참조) – Drum
JSON에서 문자열 메서드를 사용하지 마십시오. 여기서 "실용주의"라는 말이 없습니다. 파스. 그만큼. JSON. 노드에 무료 JSON 구문 분석기가 있습니다. 사용하기 쉽습니다. – Tomalak