file.load 返回“TypeError:无法调用方法“load”未定义的”在套房

发布于 2025-01-09 18:24:08 字数 3299 浏览 1 评论 0原文

问题:我试图查看文件柜中是否已存在文件。如果是这样,用户会收到警告。如果没有,则继续保存文件。这些组件的所有组件都可以工作,除了当我尝试加载文件时出现以下错误。

org.mozilla.javascript.EcmaError: TypeError: 无法调用未定义的“load”方法 (/SuiteScripts/suitelet_CheckIfFileExist.js#23)

我当前正在传递一个我知道存在的文件的文件 ID 进行测试,所以我知道我正在通过有效的文件 ID。

客户端脚本:

function checkIfFileExists(){
    CallforSuitelet_CheckIfFileExist();
    var result
    if(result == true){
        Ext.Msg.show({
        title:'Overwrite File?',
        msg: 'This existing file will be<br>overwritten:<br>' + msgBoxValue + '<br><br>Continue?',
                width: 300,
        icon: Ext.MessageBox.QUESTION,
        buttons: Ext.MessageBox.YESNO,
        fn: function(buttonId, value) {
            if (buttonId == 'yes') {
                CallforSuitelet();
                console.log('Yes pressed');
                } else {
                    Ext.Msg.show({
                    title:'Information',
                    msg: 'No files were copied.',
                    icon: Ext.MessageBox.INFO,
                    buttons: Ext.MessageBox.OK
                });
                    console.log('No pressed');
                    }
                    }
            });
        }
    }

 }
    
function CallforSuitelet_CheckIfFileExist(){
    console.log("CallforSuitelet_CheckIfFileExist function Entered")

var suiteletURL = url.resolveScript({
    scriptId:'customscript_suitelet_checkiffileexist',// script ID of your Suitelet 
    deploymentId: 'customdeploy_suitelet_checkiffileexist',//deployment ID of your Suitelet
    returnExternalURL: false,
    params: {
           'msgBoxValue':msgBoxValue
        }
    });
        console.log("suiteletURL = " + suiteletURL);

    https.get.promise({
            url: suiteletURL
            });
}

套件:

   /**
    * @NApiVersion 2.x
    * @NScriptType Suitelet
    * @NModuleScope SameAccount
    */
   define(['N/file', 'N/log'],

   function(file, log) {

/**
 * Definition of the Suitelet script trigger point.
 *
 * @param {Object} context
 * @param {ServerRequest} context.request - Encapsulation of the incoming request
 * @param {ServerResponse} context.response - Encapsulation of the Suitelet response
 * @Since 2015.2
 */

function onRequest(context) {

    if (context.request.method === 'GET') {
        
        var requestParam = context.request.parameters;
        
        var fileName = 'Drag and Drop/Sales Order/' + requestParam.msgBoxValue + '.pdf';
        
        var contextResponse = 'true';
        
  
    function fileHasNoValue(fileId){
        if (typeof fileId == 'undefined'){
        return true;
        }
        if (fileId === null){
        return true;
        }
        if (fileId === ''){
        return true;
        }
    return false;
    }
    log.debug({details:'line beforefileOBj '})
    var fileObj = file.load(288784)
    //file.load({ <======= I have tried loading both ways
    //id: 288784
    //});

    try{
        var file = file.load({id: fileName});
        contextResponse = fileHasNoValue(file);
    } catch (e) {
        log.error({
        title: e.name,
        details: e.message});
        }
    };

  context.response.write(contextResponse)
  return;
}

return {
    onRequest: onRequest
};

});

Problem: I am trying to see if a file already exists in the file cabinet. If it does, the user gets a warning. If it does not, then the file proceeds to save. All components of these work except when I try to load the file I get the following error.

org.mozilla.javascript.EcmaError: TypeError: Cannot call method "load" of undefined (/SuiteScripts/suitelet_CheckIfFileExist.js#23)

I am currently passing a file id of a file I know exist for testing so I know that I am passing a valid file id.

ClientScript:

function checkIfFileExists(){
    CallforSuitelet_CheckIfFileExist();
    var result
    if(result == true){
        Ext.Msg.show({
        title:'Overwrite File?',
        msg: 'This existing file will be<br>overwritten:<br>' + msgBoxValue + '<br><br>Continue?',
                width: 300,
        icon: Ext.MessageBox.QUESTION,
        buttons: Ext.MessageBox.YESNO,
        fn: function(buttonId, value) {
            if (buttonId == 'yes') {
                CallforSuitelet();
                console.log('Yes pressed');
                } else {
                    Ext.Msg.show({
                    title:'Information',
                    msg: 'No files were copied.',
                    icon: Ext.MessageBox.INFO,
                    buttons: Ext.MessageBox.OK
                });
                    console.log('No pressed');
                    }
                    }
            });
        }
    }

 }
    
function CallforSuitelet_CheckIfFileExist(){
    console.log("CallforSuitelet_CheckIfFileExist function Entered")

var suiteletURL = url.resolveScript({
    scriptId:'customscript_suitelet_checkiffileexist',// script ID of your Suitelet 
    deploymentId: 'customdeploy_suitelet_checkiffileexist',//deployment ID of your Suitelet
    returnExternalURL: false,
    params: {
           'msgBoxValue':msgBoxValue
        }
    });
        console.log("suiteletURL = " + suiteletURL);

    https.get.promise({
            url: suiteletURL
            });
}

Suitelet:

   /**
    * @NApiVersion 2.x
    * @NScriptType Suitelet
    * @NModuleScope SameAccount
    */
   define(['N/file', 'N/log'],

   function(file, log) {

/**
 * Definition of the Suitelet script trigger point.
 *
 * @param {Object} context
 * @param {ServerRequest} context.request - Encapsulation of the incoming request
 * @param {ServerResponse} context.response - Encapsulation of the Suitelet response
 * @Since 2015.2
 */

function onRequest(context) {

    if (context.request.method === 'GET') {
        
        var requestParam = context.request.parameters;
        
        var fileName = 'Drag and Drop/Sales Order/' + requestParam.msgBoxValue + '.pdf';
        
        var contextResponse = 'true';
        
  
    function fileHasNoValue(fileId){
        if (typeof fileId == 'undefined'){
        return true;
        }
        if (fileId === null){
        return true;
        }
        if (fileId === ''){
        return true;
        }
    return false;
    }
    log.debug({details:'line beforefileOBj '})
    var fileObj = file.load(288784)
    //file.load({ <======= I have tried loading both ways
    //id: 288784
    //});

    try{
        var file = file.load({id: fileName});
        contextResponse = fileHasNoValue(file);
    } catch (e) {
        log.error({
        title: e.name,
        details: e.message});
        }
    };

  context.response.write(contextResponse)
  return;
}

return {
    onRequest: onRequest
};

});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从﹋此江山别 2025-01-16 18:24:08

去过那里,做过那件事。

通过使用 var 关键字,您将掩盖引用文件模块的变量。在 Javascript 中,var 关键字具有函数作用域,因此您所做的相当于:

function(onContext){
    var file;
    if (context.request.method === 'GET') {
    ...
    file = file.load... // if you look at it this way it's obvious that file no longer has a reference to the file module. 
}

其中一个应该有效:

file = file.load({id: fileName}); // file refers to the file module until it gets reassigned. 
contextResponse = fileHasNoValue(file);

或者

var myFile = file.load({id: fileId});
contextResponse = fileHasNoValue(myFile);

Been there, done that.

By using the var keyword you are overshadowing the variable that references the file module. In Javascript the var keyword has function scope so what you are doing is equivalent to:

function(onContext){
    var file;
    if (context.request.method === 'GET') {
    ...
    file = file.load... // if you look at it this way it's obvious that file no longer has a reference to the file module. 
}

Either of these should work:

file = file.load({id: fileName}); // file refers to the file module until it gets reassigned. 
contextResponse = fileHasNoValue(file);

or

var myFile = file.load({id: fileId});
contextResponse = fileHasNoValue(myFile);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文