MissingSchemaError: Schema hasn’t been registered for model “User”

In my models/user.js file: And in my router/index.js, I have: which throws the error: If however, in user.js, I do (in the last line) and in index.js I do var User = require(‘../models/User’);, then everything works. But it should not, because in config/pass.js I am doing var User = mongoose.model(‘User’); and it’s working flawlessly. The … Read more

Expressjs / Node.js – res.redirect() not loading page

Your request is POST ($.post) and you route is app.del, so it never gets to res.redirect inside app.del route. Why don’t you use app.post? Updated: Assuming $.post sends HTTP DEL request here what is happening: server sends 302 response with no data but browser never sends another request to GET route as server instructs it … Read more

Difference between MongoDB and Mongoose

I assume you already know that MongoDB is a NoSQL database system which stores data in the form of BSON documents. Your question, however is about the packages for Node.js. In terms of Node.js, mongodb is the native driver for interacting with a mongodb instance and mongoose is an Object modeling tool for MongoDB. mongoose is built on top of the mongodb driver to provide programmers … Read more

Push items into mongo array via mongoose

Assuming, var friend = { firstName: ‘Harry’, lastName: ‘Potter’ }; There are two options you have: Update the model in-memory, and save (plain javascript array.push): or I always try and go for the first option when possible, because it’ll respect more of the benefits that mongoose gives you (hooks, validation, etc.). However, if you are doing … Read more

(node:63208) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead

The issue is that mongoose still uses collection.ensureIndex and should be updated by them in the near future. To get rid of the message you can downgrade by using version 5.2.8 in your package.json (and delete any caches, last resort is to uninstall it the install it with npm install [email protected]): “mongoose”: “^5.2.8” EDIT: As of this edit, Mongoose … Read more

Mongoose – What does the exec function do?

Basically when using mongoose, documents can be retrieved using helpers. Every model method that accepts query conditions can be executed by means of a callback or the exec method. callback: exec: Therefore when you don’t pass a callback you can build a query and eventually execute it. You can find additional info in the mongoose docs. UPDATE Something to note … Read more

Mongoose: findOneAndUpdate doesn’t return updated document

Why this happens? The default is to return the original, unaltered document. If you want the new, updated document to be returned you have to pass an additional argument: an object with the new property set to true. From the mongoose docs: Query#findOneAndUpdate Available options new: bool – if true, return the modified document rather than the original. defaults to false (changed in 4.0) Solution Pass {new: true} if you … Read more