【问题标题】:How to update warning in webstorm?如何在 webstorm 中更新警告?
【发布时间】:2015-07-28 18:09:08
【问题描述】:

我一直在开发 node js 应用程序,它使用 mongodb 来操作静态数据。我正在将 webstorm 用于我的主编辑器程序。

我写了一个函数,它使用 mongodb 来查找数据。其实这是mongodb入门文档中的示例代码。

var findRestaurants = function(db, callback) {
    var cursor =db.collection('restaurants').find( );
    cursor.each(function(err, doc) {
        assert.equal(err, null);
        if (doc != null) {
            console.dir(doc);
        } else {
            callback();
        }
    });
};

第三行each是mongodb驱动api中定义的游标对象的方法。该方法对返回的光标指向的记录进行回调函数,运行这段代码没有问题。

但是,在 webstorm 编辑器窗口中,程序对 each 方法发出警告,说这个符号已被弃用。它说 javascript 已弃用此 each 方法。貌似webstorm不知道node js或者mongodb的api信息。当然,我可以忽略这条消息,但这让我有点恼火。

有没有办法更新webstorm程序的警告信息?我觉得有办法注册node js或者mongodb api list到webstorm程序,但是搜索找不到。

谢谢。

【问题讨论】:

  • 我看不到此代码显示的任何警告。这个错误是什么样的?

标签: webstorm deprecated


【解决方案1】:

我遇到了同样的问题,我发现它是对实际上已弃用的 javascript .each() 方法的引用。解决此问题的最简单(阅读快速修复)方法是将“//noinspection JSDeprecatedSymbols”放在给出错误的行上方。您也可以单击左侧的灯泡图标,转到“检查已弃用的 Javascript 符号选项”,然后禁止声明(或您可能希望用来禁用此警告的任何其他选项)

var findRestaurants = function(db, callback) {
    var cursor =db.collection('restaurants').find( );
    //noinspection JSDeprecatedSymbols
    cursor.each(function(err, doc) {
        assert.equal(err, null);
        if (doc != null) {
            console.dir(doc);
        } else {
            callback();
        }
    });
};

【讨论】:

    【解决方案2】:

    我查看了 Node.JS 的 MongoDB 引擎中存在的函数列表。有两个函数可以替代“each”函数:“next”和“hasNext”。

    因此,对于您的示例,我将编写以下代码:

    var findRestaurants = function(db, callback) {
        var cursor = db.collection('restaurants').find( );
        var parseRestaurant = function() {
            cursor.next(function(err, restaurant){
                if (err)
                    return console.error(err);
                console.dir(doc);
                hasNextRestaurant();
            });
        };
    
        var hasNextRestaurant = function() {
            cursor.hasNext(function(err, result){
                if (err)
                    return console.error(err);
    
                if (result)
                    parseRestaurant();
                else {
                    //Here is the last point of iterations
                    //We can use this for statistics output
                    return console.log('That\'s all');
                }
            });
        };
    
        hasNextRestaurant();
    }
    

    或者是这样的:

    var findRestaurants = function(db, callback) {
        var cursor = db.collection('restaurants').find( );
        var parseRestaurant = function(err, doc) {
            if (err)
                return console.error(err);
            console.dir(doc);
            cursor.hasNext(checkNext);
        };
    
        var checkNext = function(err) {
            if (err)
                return console.error(err);
    
           if (result)
                parseRestaurant();
            else {
                //Here is the last point of iterations
                //We can use this for statistics output
                return console.log('That\'s all');
            }
            cursor.next(parseRestaurant);
        };
    
        cursor.hasNext(checkNext);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-11
      • 2018-01-07
      • 2020-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多