我正在编写连接到 mongo 数据库的节点脚本。
我注意到打印错误响应因console.log
语法的微小变化而有所不同。下面的例子应该更有意义。
#!/usr/bin/env node
const mongoose = require("mongoose");
const config = require("./config");
mongoose.connect(config.dbURI, (err) => {
if (err) {
console.log(`${err}`); // First console.log
console.log(err); // Second console.log
} else {
console.log(`Database connection successful`)
}
});
第一个示例输出console.log
MongoError: Authentication failed.
第二个示例输出console.log
{ MongoError: Authentication failed.
at Function.MongoError.create (/Users/sigo/Sources/crypto-change/node_modules/mongodb-core/lib/error.js:31:11)
at /Users/sigo/Sources/crypto-change/node_modules/mongodb-core/lib/connection/pool.js:489:72
at authenticateStragglers (/Users/sigo/Sources/crypto-change/node_modules/mongodb-core/lib/connection/pool.js:435:16)
at Connection.messageHandler (/Users/sigo/Sources/crypto-change/node_modules/mongodb-core/lib/connection/pool.js:469:5)
at Socket.<anonymous> (/Users/sigo/Sources/crypto-change/node_modules/mongodb-core/lib/connection/connection.js:321:22)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at Socket.Readable.push (_stream_readable.js:136:10)
at TCP.onread (net.js:563:20)
name: 'MongoError',
message: 'Authentication failed.',
ok: 0,
code: 18,
errmsg: 'Authentication failed.' }
这种差异从何而来?