1

I'm writing node's script that connect to the mongo database.

I noticed that printing error response is different by small change in console.log syntax. Below example should be more meaningful.

#!/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`)
    }
});

Example output of the first console.log

MongoError: Authentication failed.

Example output of the second 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.' }

Where comes from this difference?

4
  • What does “Where comes from this difference?” mean?
    – dumbass
    Commented Apr 21 at 10:40
  • @dumbass Are you asking how to parse the sentence in English? If so, it means "where does this difference come from". If you're asking what OP is referring to by "difference", they mean compare the output of the first console.log to the output of the second console.log.
    – TylerH
    Commented Apr 21 at 15:03
  • @TylerH You cannot possibly know that for sure, so you should not pretend your guesses are facts.
    – dumbass
    Commented Apr 21 at 15:19
  • @dumbass It's not a guess, it's English grammar. It's trivial for a native English speaker to understand someone saying "Where comes from this difference" is trying to say "Where does this difference come from" but doesn't understand how to correctly order verbs in English because their native language puts them in a different order (in this case, Polish).
    – TylerH
    Commented Apr 21 at 15:20

2 Answers 2

2

${err} (or '' + err for that matter) will interpolate the err object with a string which will also change your Error object into a string - which is equivalent to calling err.toString(). However directly logging err will pass it to the console as an object and display it in that manner.

This is called "implicit coercion" - if you would like to know more about this subject I can highly recommend reading You Don't Know JS: Types & Grammar (Chapter 4: Coercion) for a deep dive.

0
2

By interpolating in a template string, the err object is cast to a string, using its own .toString method that is inherited from Error.prototype.toString. The code is equivalent to

console.log(String(err));
console.log(err.toString());

When passing the err object directly to the console.log method, it uses node's inspect function instead, the code is equivalent to

console.log(util.inspect(err));

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.