在 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();
})
})
这样程序就会被停止运行,但是不终止,就能在控制台看到展开的对象:

网友评论