Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Node.js and Mongoose: Connection Hanging On find() [Code Snippet]

DZone's Guide to

Node.js and Mongoose: Connection Hanging On find() [Code Snippet]

A quick code snippet showing you how to debug some MongoDB errors when building REST APIs on Node.js with Express.

· Web Dev Zone
Free Resource

Get the senior executive’s handbook of important trends, tips, and strategies to compete and win in the digital economy.

Following this tutorial to build a simple REST API on Node.js with Express, attempting to retrieve all docs from my MongoDB gave these errors:

2016-05-19T20:10:47.311-0700 [initandlisten] connection accepted from 127.0.0.1:52053 #4 (2 connections now open)

2016-05-19T20:10:47.325-0700 [conn4]  authenticate db: nodetest { authenticate: 1, user: "", nonce: "xxx", key: "xxx" }

2016-05-19T20:10:47.336-0700 [conn4] Failed to authenticate @nodetest with mechanism MONGODB-CR: ProtocolError field missing/wrong type in received authenticate command

Some quick Googling turned up the same error in this post that just suggested authentication was failing because no user was passed. So created a new used with:

db.createUser({user : 'nodetest', pwd: 'passwordhere', roles: ['readWrite', 'nodetest']})

… and problem solved.

Related Refcard:

Read this guide to learn everything you need to know about RPA, and how it can help you manage and automate your processes.

Topics:
mongoose ,node.js ,rest ,api ,express ,mongodb

Published at DZone with permission of Kevin Hooke, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}