javascript文件出错,但控制台没有

Error in javascript file but not console?

本文关键字:控制台 文件 出错 javascript      更新时间:2023-09-26

问题中的错误

Uncaught Error: The property or field has not been initialized. 
It has not been requested or the request has not been executed. 
It may need to be explicitly requested. 

这是代码,只是试图获得一个列表的简单计数。

var CustomAction = function(){
    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web(); 
    this.oList = web.get_lists().getByTitle("Classification");
    // .load() tells CSOM to load the properties of this object
    // multiple .load()s can be stacked
    clientContext.load(oList);
    // now start the asynchronous call and perform all commands
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFailure));
    // method will exit here and onSuccess or OnFail will be called asynchronously
};
function onSuccess(sender, args) {
    alert('No of rows: ' + oList.get_itemCount());
};
function onFail(sender, args) {
    alert('Request failed.'n' + args.get_message() + ''n' + args.get_stackTrace());
};

错误发生在oList.get_itemCount()。发生这种情况的原因是什么?我尝试使用$( document).ready$(window).onload,但问题仍然存在。就像我说的,当我把它复制/粘贴到浏览器中时,它是有效的,但从文件中运行它就不行了。

请改用

var CustomAction = function(){
    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web(); 
    this.oList = web.get_lists().getByTitle("Classification");
    // .load() tells CSOM to load the properties of this object
    // multiple .load()s can be stacked
    clientContext.load(oList);
    // now start the asynchronous call and perform all commands
    clientContext.executeQueryAsync((function(sender, args){alert('No of rows: ' + oList.get_itemCount())}(this, this.onSuccess)), (function(sender, args){alert('Request failed.'n' + args.get_message() + ''n' + args.get_stackTrace())}(this, this.onFailure)));
};