美文网首页
vscode 的 Debug Console 控制台中对象和数组

vscode 的 Debug Console 控制台中对象和数组

作者: 般犀 | 来源:发表于2018-12-21 00:44 被阅读0次

    在 vscode 做 node 的一些测试的时候,想看看 console 出来的对象里有什么东西,却发现打印出来的对象无法被展开,像这样:


    对象无法被展开

    找了下是什么原因,在 vscode 的 issue 下也有人提出这个问题,vscode 成员的回答是:只有当程序还在运行中时,才能对打印出来的对象进行展开。也就是说对只运行一次的脚本程序是看不到对象的展开的。

    那么有没有什么方法让程序一直保持运行呢?方法就是在脚本里加一个 debugger即可:

    const MongoClient = require('mongodb').MongoClient;
    const url = 'mongodb://localhost:27017/mydb';
    
    MongoClient.connect(url, function(err, db) {
      if (err) throw err;
      const dbo = db.db('mydb');
      dbo.collection('customers').find({}).toArray((err, res) => {
        if (err) throw err;
        console.log(res);
        debugger
        db.close();
      })
    })
    

    这样程序就会被停止运行,但是不终止,就能在控制台看到展开的对象:


    对象里的内容可以被看到了!

    参考资料:
    Object does not expand in Debug Console #44806

    相关文章

      网友评论

          本文标题:vscode 的 Debug Console 控制台中对象和数组

          本文链接:https://www.haomeiwen.com/subject/mxepkqtx.html