Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript browserify can';t调用函数i bundle,它';没有定义_Javascript_Node.js_Upload_Browserify - Fatal编程技术网

Javascript browserify can';t调用函数i bundle,它';没有定义

Javascript browserify can';t调用函数i bundle,它';没有定义,javascript,node.js,upload,browserify,Javascript,Node.js,Upload,Browserify,我有一些问题,我想捆绑以下node.js文件项目,我在以下代码中修改了该文件,并在upload.js的同一目录中调用了upload2.js文件: var SketchfabDataApi = require( '../../index' ); var Swagger = require('swagger-client'); var fs = require('fs'); var path = require('path'); var api = new SketchfabDataAp

我有一些问题,我想捆绑以下node.js文件项目,我在以下代码中修改了该文件,并在upload.js的同一目录中调用了upload2.js文件:

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命令

browserify upload2.js -o bundleSketchFabDataApi.js -d
下面是我的call.js脚本:

   <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");

............................
上传ModelBySketchFabdataAPI(“mytoken”、“myfile”);
但我总是在控制台上出现相同的错误“Reference is undefined”:

更新 Ty to dnitro建议现在我可以使用窗口变量访问我的函数,但我必须继续使用browserify出错,因为现在我的机器看不到我的fs模块返回文本fs。createReadStream不是屏幕截图中的函数:


请提前提出建议。

Browserify不允许变量污染全局范围。如果您想使用一个,您应该将其附加到全局

如果希望
窗口
可以使用
UploadModelBySketchfabdataApi
函数,可以将其附加到:

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

更新

npm install browserify-fs
Browserify不支持fs模块。看

你可以用哪一种用法。他们声称:

fs模块中的所有异步方法都受支持并经过良好测试 (包括链接!)

但要注意浏览器支持:

安装

npm install browserify-fs
用法

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.json替换它:

var fs = require('fs');

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


答案已更新。