2016-06-21 7 views
0

私のユーザインタフェース(angularjs)では新しい行を作成します。各行にファイルアップロードボタンがあります。私はすべてのファイルをメタデータとともにアップロードし、各行を1回の呼び出しで保存したいと思います。 Nodejs APIに投稿する複合オブジェクトは、以下のようになります。Nodejs API - multer fileupload - JSONオブジェクトにプロパティとその値を動的に追加

var activity = { 
    "Id" : 1, 
    "Name" : "Test", 
    "Steps" : [ 
    { 
     "StepId":1, 
     "FileUrl": {fileObject} // this property if bound with the file upload directive 'ng-file-upload' by Daniel Farid 
     "Description" : "Save this file" 
     }, 
    { 
     "StepId":2, 
     "FileUrl": {fileObject} // this property if bound with the file upload directive 'ng-file-upload' by Daniel Farid 
     "Description" : "Save this file2" 
     } 
    ] 
} 

このJSONはノードjs APIに送信されます。 Nodejs側では、multerを使用してアップロードしたファイルをサーバーに保存しています。 multerの.any()メソッドを使用してAPI内のすべてのファイルを取得しますが、Steps [x] .FileUrlプロパティを使用せずに投稿オブジェクトを取得します。

このファイルが追加されたフィールド名に関する情報を持つファイルオブジェクト。以下は私がデバッガで見る情報です。

Array[2] 
length:2 
[0]:Object 
destination:"C:\DeleteThis\" 
encoding:"7bit" 
fieldname:"Steps[0][FileUrl]" 
filename:"ed13d2a61cb38c43f1f46a221855a896" 
mimetype:"image/png" 
originalname:"deploy.png" 
path:"C:\DeleteThis\ed13d2a61cb38c43f1f46a221855a896" 
size:2347 
[1]:Object 

は、今私が掲示されて私の複雑なオブジェクトは、ステップ[0] .FileUrlプロパティを持っていないので、それを何をしたいのか、私は(すなわちreq.files)ファイルごとに反復してにフィールド名を使用したいですこのプロパティを作成し、originalNameを値として割り当てます。私はそれを

var deployment = req.body; 
     if(req.files){ 
      var app = _config.getApplicationConfig(req.body.ApplicationId); 
      req.files.forEach(function(f){ 

       //Move file to the deployment folder. 
       _utils.createDirIfNotExist(app.packageDir); 
       var newPath = _utils.DetermineFileName(f.originalname, app.packageDir); 
       _fs.renameSync(f.path, path.join(app.packageDir,newPath)); 
       var newFileName = path.basename(newPath); 
       //set the file url to corresponding field 
       var evalExp = "deployment." + f.fieldname; //I get evalExpression as "deployment.Steps[0][FileUrl]" 
       eval(evalExp); //Here it fails saying FileUrl is not defined 
       evalExp = "deployment." + f.fieldname + "= \"" + newFileName.toString() + "\""; 
       eval(evalExp);  
      }); 
     } 

をやろうとしていますどのように

誰でものように、実行時にオブジェクトにプロパティを割り当てることができる方法を知っていますか?

答えて

0

私はこれを以下のように解決しました。 []表記をに変換する関数を書きました。表記法。 myobj [myprop] to myobj.myprop

var convertToDotNotation = function (keyPath) { 
    var bracketSyntaxRegex = new RegExp(/\[([^0-9])\w+\]/g); //matches the javascript property defined in [] syntax but not an array 
    var matches = keyPath.match(bracketSyntaxRegex) 
    if(matches && matches.length > 0){ 
     matches.forEach(function(p){ 
      //replace '[' with '.' and ']' with emptyspace 
      var dotSyntax = p.replace("[",".").replace("]",""); 
      keyPath = keyPath.replace(p,dotSyntax); 
     }); 
    } 
    return keyPath; 
} 

これは私に '。プロパティを動的に作成して値を設定できる表記

var newFileName = "MyFile.pdf"; 
var evalExp = "deployment[0].[FileUrl]" ; 
var temp = convertToDotNotation(evalExp); 
eval(temp + "= \"" + newFileName + "\""); 

誰かを助けることを願っています。

関連する問題