Javascript 如何使用nodejs编写/更新新的JSON文件

Javascript 如何使用nodejs编写/更新新的JSON文件,javascript,angularjs,json,node.js,Javascript,Angularjs,Json,Node.js,我有一些包含对象数组的JSON文件。我想知道如何更改此对象之一,从而更改JSON文件(覆盖旧文件)。我知道我不能用AngularJS来实现这一点,但是在服务器端使用NodeJS。 这就是我想做的: ---Animazione.json--- ---index.html-->(调用函数doPost();) ---index.js-(服务器) 如您所见,我试图在一个新的JSON文件中编写对象数组,但没有结果 我做错了什么?fs.writeFileSync(Animazione,JSON.string

我有一些包含对象数组的JSON文件。我想知道如何更改此对象之一,从而更改JSON文件(覆盖旧文件)。我知道我不能用AngularJS来实现这一点,但是在服务器端使用NodeJS。 这就是我想做的:

---Animazione.json---

---index.html-->(调用函数doPost();)

---index.js-(服务器)

如您所见,我试图在一个新的JSON文件中编写对象数组,但没有结果

我做错了什么?

fs.writeFileSync(Animazione,JSON.stringify(content))

编辑:

因为fs.writefile是一个传统的异步回调-您需要遵循promise规范,并返回一个新的promise,其中包含解析和拒绝处理程序,如下所示:

  return new Promise(function(resolve, reject) {
    fs.writeFile("<filename.type>", data, '<file-encoding>', function(err) {
        if (err) reject(err);
        else resolve(data);
    });    
});
writeFileSync(Animazione,JSON.stringify(content))

编辑:

因为fs.writefile是一个传统的异步回调-您需要遵循promise规范,并返回一个新的promise,其中包含解析和拒绝处理程序,如下所示:

  return new Promise(function(resolve, reject) {
    fs.writeFile("<filename.type>", data, '<file-encoding>', function(err) {
        if (err) reject(err);
        else resolve(data);
    });    
});

完整代码:我认为我们不需要你的完整代码。事实上,您可能应该编辑问题并删除代码,只留下相关的位。至于“没有结果”,你能详细说明一下吗?你的意思是
console
调用甚至不运行吗?看这里:完整的代码:我认为我们不需要你的完整代码。事实上,您可能应该编辑问题并删除代码,只留下相关的位。至于“没有结果”,你能详细说明一下吗?你的意思是
console
调用甚至不运行吗?看这里:为什么同步写入?为什么同步写入?
App.controller('AnimazioneController', ['$scope','$http', function($scope, $http) {

                    $http.get('Animazione.json').success(function(response)
                    {
                        $scope.myData=response;
                    })
                    .error(function()
                    { 
                        alert("Si è verificato un errore!"); 
                    });

                    /*Quantita'*/
                    var dataobj =
                                {
                                    'Locandina':$scope.Locandina,
                                    'Titolo':$scope.Titolo,
                                    'Regista':$scope.Regista,
                                    'Eta':$scope.Eta,
                                    'Durata':$scope.Durata,
                                    'Formato':$scope.Formato,
                                    'Data':$scope.Data,
                                    'Prezzo':$scope.Prezzo,
                                    'Quantita':$scope.Quantita
                                };

                    $scope.doPost = function()
                    {

                      $http.post(dataobj + '/resource', {param1: 1, param2: 2});
                    };


}]);
//In server (NodeJS)
var fs = require('fs'); //File system module

server.post('/resource', function(request, response)
{ //Every HTTP post to baseUrl/resource will end up here
    console.info('Updated myJSON.json');
    fs.writeFile('myJSON.json', request.body, function(err)
    {
        if (err)
        {
            console.error(err);
            return response.status(500).json(err); //Let the client know something went wrong
        }
        console.info('Updated myJSON.json');
        response.send(); //Let the client know everything's good.
    });
});
  return new Promise(function(resolve, reject) {
    fs.writeFile("<filename.type>", data, '<file-encoding>', function(err) {
        if (err) reject(err);
        else resolve(data);
    });    
});
.then(function(results) {
    return new Promise(function(resolve, reject) {
            fs.writeFile(ASIN + '.json', JSON.stringify(results), function(err) {
               if (err) reject(err);
               else resolve(data);
            });
    });
  }).then(function(results) {
       console.log("results here: " + results)
  }).catch(function(err) {
       console.log("error here: " + err);
  });