browserify 无法调用我捆绑的函数,它没有定义
browserify can't call the function i bundle, it's not defined
我在使用 browserify , i want to bundle the following node.js file project upload.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");
但我在控制台上总是出现相同的错误“引用未定义”:
更新
Ty dnitro 建议现在我可以使用 window 变量访问我的函数但是我必须继续使用 browserify 做一些错误因为现在我的机器看不到我的 fs 模块 return 文本 fs.createReadStream 不是屏幕截图中的函数 :
如有任何建议,请提前联系。
Browserify 不允许变量污染全局范围。如果你想使用一个,你应该将它附加到全局变量。
这里如果你想要 UploadModelBySketchfabdataApi
功能对 window
可用,你可以附上它:
window.UploadModelBySketchfabdataApi = function (token, idinputfile) { ...
更新:
Browserify 不支持 fs 模块。参见 compatibility list。
您可以使用 browserify-fs which uses level-filesystem。他们声称:
All async methods in the fs module are supported and well tested
(including links!)
但要注意浏览器支持:
安装:
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"
}
我在使用 browserify , i want to bundle the following node.js file project upload.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");
但我在控制台上总是出现相同的错误“引用未定义”:
更新
Ty dnitro 建议现在我可以使用 window 变量访问我的函数但是我必须继续使用 browserify 做一些错误因为现在我的机器看不到我的 fs 模块 return 文本 fs.createReadStream 不是屏幕截图中的函数 :
如有任何建议,请提前联系。
Browserify 不允许变量污染全局范围。如果你想使用一个,你应该将它附加到全局变量。
这里如果你想要 UploadModelBySketchfabdataApi
功能对 window
可用,你可以附上它:
window.UploadModelBySketchfabdataApi = function (token, idinputfile) { ...
更新:
Browserify 不支持 fs 模块。参见 compatibility list。
您可以使用 browserify-fs which uses level-filesystem。他们声称:
All async methods in the fs module are supported and well tested (including links!)
但要注意浏览器支持:
安装:
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"
}