2017-02-02 10 views
1

browserifyの問題があります。次のnode.jsファイルプロジェクトupload.jsをバンドルしたい場合は、次のコードでファイルを変更してファイルupload2を呼び出します。 upload.jsの同じディレクトリにJS:browserifyは、関数iバンドルを呼び出すことができません。定義されていません。

var SketchfabDataApi = require('../../index');  
var Swagger = require('swagger-client'); 
var fs = require('fs'); 
var path = require('path'); 
var api = new SketchfabDataApi(); 

    function UploadModelBySketchfabdataApi(token,idinputfile) { 
    //var file = jQuery(idinputfile)[0].files[0]; 
    var file = document.getElementById(idinputfile).files[0] 
    if (file) { 
     var fileName = file.name; 
    } 
    var fullPathFile = document.getElementById(idinputfile).value; 

    //var fullPathFile = window.location.protocol + "//" + window.location.host; 
    //if (window.location.port != "") fullPathFile += ":" + window.location.port + "/"; 
    //fullPathFile = fullPathFile + '/private/' + fileName; 

    console.info('START UPLOAD2:' + fullPathFile); 

    api.then(function (client) { 

    // This is how you authenticate your requests with the "Token" scheme 
    client.clientAuthorizations.add("Token", 
     new Swagger.ApiKeyAuthorization(
      "Authorization", 
      "Token " + token , 
      "header" 
     ) 
    ); 

    // This is how you upload a file 
    client.apis.models.post_v3_models({ 
     isPublished: 'false', 
     modelFile: fs.createReadStream(path.resolve(fullPathFile)), 
     private:false, 
    }).then(function (response) { 

     if (response.status === 201) { 
      // The model URI is immediately returned, even if processing hasn't finished yet 
      console.log('After processing, model will be available at: ' + 
       response.headers.location); 
      var uid = response.headers.location 
       .replace('https://api.sketchfab.com/v3/models/', ''); 

      // You can poll the processing status to know when the model is ready 
      // See how `pollStatus` is implemented below 
      pollStatus(client, 
       uid, 
       function (err, res) { 
        console.log(err, res); 
       }); 
      window.location.href = window.location.protocol + "//" + window.location.host + "/stealth/#/stealth/models3d/models3d"; 
     } 


    }).catch(function (error) { 
     console.error("ERROR ON UPLAOD:" + error); 
    }); 

}).catch(function (error) { 
    console.log("ERROR ON AUTHENTICATION:" + error); 
}); 

} 

    /** 
    * Poll processing status 
    * @param {object} client Swagger client 
    * @param {string} uid Model uid 
    * @param {function} callback will receive (err, result) 
    */ 
    function pollStatus(client, uid, callback) { 
     client.apis.models.get_v3_models_uid({ 
      uid: uid 
     }).then(function (response) { 
      if (response.obj.status.processing === 'SUCCEEDED') { 
       callback(null, response.obj.status); 
      } else if (response.obj.status.processing === 'FAILED') { 
       callback(response.obj.status.processing, null); 
      } else { 
       setTimeout(function() { 
        console.log(response.obj.status); 
        pollStatus(client, uid, callback); 
       }, 1000); 
      } 
     }); 
    } 

は、今私は

browserify upload2.js -o bundleSketchFabDataApi.js -d 

、ここで私のcall.jsスクリプト、browserifyのコマンドを実行します。

<script type="text/javascript" src="vendor/sketchfab/SketchfabDataApi/bundleSketchFabDataApi.js"></script> 
<script type="text/javascript" src="vendor/sketchfab/SketchfabDataApi/SketchfabDataApi.js"></script> 
............................ 
    UploadModelBySketchfabdataApi("mytoken", "myfile"); 

しかし、私は「リファレンスが未定義である」常にコンソールに同じエラーを持っている:dnitro提案へ enter image description here

UPDATE のTy今私は、ウィンドウの変数で私の機能にアクセスすることができますが、fs モジュールfs.createReadStreamは関数ではありませんスクリーンショットのように のようになりました。

事前にtyをアドバイスしてください。

+0

回答が更新されました。 – dNitro

答えて

2

Browserifyでは変数がグローバルスコープを汚染することはできません。 1つを使用する場合は、グローバルに添付する必要があります。ここでは、windowに利用できるUploadModelBySketchfabdataApi機能が必要な場合は

、あなたはそれを添付することができます:

window.UploadModelBySketchfabdataApi = function (token, idinputfile) { ... 


更新

Browserifyがfsのモジュールをサポートしていません。 compatibility listを参照してください。

browserify-fsにはlevel-filesystemを使用できます。彼らはそれを主張します:

fsモジュール内のすべての非同期メソッドはサポートされ、よくテストされます (リンクを含む!)

しかし、ブラウザのサポートのために気を付ける:

enter image description here

インストール

npm install browserify-fs 

を使用:直接

var fs = require('browserify-fs'); 

または正規fsモジュールを使用して、バンドル時にbrowserify-fsに置き換えます

var fs = require('fs'); 

// CLI 
browserify main.js -r fs:browserify-fs -o bundle.js 

または正規fsモジュールを使用し、それを交換するpackage.jsonbrowser filedを使用します。

var fs = require('fs'); 

// package.json 
"browser": { 
    "fs": "browserify-fs" 
} 

関連する問題