2014-11-16 2 views
0

express 3.5 js를 사용하고 있습니다. 내가 뭘 하려는지, 양식 요소를 JSON으로 변수 데이터에 저장하고 서버로 다시 보내고 서버의 JSON을 업데이트하는 것입니다.express js를 사용하여 서버에서 json을 업데이트하는 방법

브라우저에서 json 출력을 { "name-check": true, "name-text": "someName", "dest-check": false, "destination-text": "someplace"}로 얻습니다.

app.post('/json/*.json', function(req, res) { 
var outputFilename = '/tmp/my.json'; 
var mydata = req.body; 

fs.writeFile(outputFilename, mydata , function(err) { 
    if (err) { 
     console.log(err); 
    } else { 
     console.log("JSON saved to " + outputFilename); 
    } 
}); 

json을 양식의 데이터로 업데이트하려고하면 작동하지 않습니다.

같은 파일에있는 데이터가있는 한이 게시물 How can I pretty-print JSON using node.js?을 시도했지만 양식에서 가져 오는 경우 작동하지 않습니다.

+0

req.body.namecheck (이름 확인 대신)를 할 때 값을 볼 수 있습니다. 하지만 javascript 변수에 { "name-check": true, "name-text": "someName", "dest-check": false, "destination-text": "someplace" "} – user2950017

+0

나는 이것을 작동시킬 수 있었다. – user2950017

답변

0

먼저 변수에서 req.body를 가져 와서 JSON.stringify를 사용했습니다.

관련 문제