Mongoose - find() method isn't working with second collection - node.js

I have two models in my project - User and Game. The following code behaves very strangely:
var mongoose = require('../lib/mongoose.js');
var User = require('../models/user').User;
var Game = require('../models/game').Game;
User.count(function(err, count) {
console.log("users count: " + count); //prints 2
});
Game.count(function(err, count) {
console.log("games count: " + count); //prints 3
});
User.find(function(err, users) {
if(err) throw err;
console.log("users: ");
console.log(users); //prints all users
});
Game.find(function(err, games) {
if(err) throw err; //no error throwed
console.log("games: "); //This never runs
console.log(games);
});
It's all right with User (logs all users as expected), but Game.find never calls callback.
Another one interesting detail: if "Game" collection is empty, find works properly, prints empty array. But when there are any documents in it, it stops working. Also it doesn't work with mongoose queries.
Also tried to create model with passing third argument like this:
exports.Game = mongoose.model('Game', schema, 'Game');
But it also works fine only with empty collection
Mongoose module if you need it
var mongoose = require('mongoose');
var config = require('../config');
mongoose.Promise = global.Promise;
mongoose.connect(config.get('mongoose:uri'), config.get('mongoose:options'));
module.exports = mongoose;

Related

Find not working with mongoose 4.6.*

Using mongoose if I try to find a document stored in the database using db.model() like mongoose docs suggests, I get no results :(
However, if I use db.collection().find() I get results.
Why is that? and what will be the best approach using the example code below?
In app.js
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(config.database.url); // database configuration
app.locals.db = mongoose.connection;
app.locals.db.on('error', function(msg){
console.log("db connection failed", msg);
});
app.locals.db.once('open', function(){
console.log("db connected successfully");
});
In routes -> person.js
var Person = require('Person');
router.post('/search', function(req, res) {
var person = new Person(req.app.locals.db);
person.getPerson(req.body.name, function(found, docs) {
if (found === false)
res.render('pageTpl', { results: 'person not found' });
else
res.render('pageTpl', { results: docs });
});
In Person.js
// schema here returns new mongoose.Schema({ fields : types })
const personSchema = require('./schemas/personSchema');
function Person(db) {
this.db = db;
}
Person.prototype.getPerson = function(term, callback) {
var Person = this.db.model('people', personSchema);
var q = Person.find({ name: /^term/ }).sort('age').limit(2);
q.exec(function(err, results) {
if (err) return callback (false, err);
// This returns [] results
console.log(results);
callback(true, results);
});
});
module.exports = Person;
The problem is with this line: var Person = this.db.model('people', personSchema);
Mongoose automagically converts model to plural so try the following: var Person = this.db.model('person', personSchema);
After some more research I figure out why I was not getting results. It all fall into the details. In Person.js I have the following line:
var q = Person.find({ name: /^term/ }).sort('age').limit(2);
If you look closely, the problem resides in here /^term/ where term in a variable but does not get translate into its value. Instead I did:
var regTerm = new RegExp("^" + term);
Now, regTerm can be used as regular expession within find.
Hope this helps someone in the future.

Retrieve data from MongoDB and save it to global object in Node.js and Express.js

I'm trying to get data from MongoDB collection and then save it to a global object.Later I need to parse it to HTML template.
Here is my code:
When user log onto his profile: then we need to get his projects and here we call findeprojects() function
usrRouter.route('/profile')
.all(function (req,res,next) {
if(!req.user){
res.redirect('/');
}
next();
})
.get(function (req,res,userObj) {
// var proj = findprojects();
userObj = req.user;
var pro = {};
pro = findprojects(userObj);
res.render('index',{name:userObj.username, email:userObj.email});
//res.sendFile('profile.html',{root:path.join(__dirname,'../public'),},{name:userObj.username});
});
Here is findeprojects function code:
var findprojects = function(obj) {
var usern = obj.username;
mongodb.connect(url,function(err, db){
if(err) throw err;
var collection = db.collection('projects');
//console.log(usern);
collection.find({'pusername':usern});
cursor =db.collection('projects').find({ 'pusername': usern }).toArray(function(err,items){
//console.log(items);
var i;
for(i=0; i<items.length;){
userProjects.createdBy = items[i].pusername;
userProjects.proName = items[i].projectName;
userProjects.proType = items[i].projectType;
userProjects.proDesc = items[i].projectDesc;
//return userProjects;
i = i+1;
}
});
console.log(userProjects);
});
};
I have declared global object at the top like:
userProjects = {
createdBy:'',
proName:'',
proType:'',
proDesc:''
};
But when I console userprojects object after calling the findeprojects() function it displays empty values.
why dont you use mongoose to model your stuff.
its more intuitive and you no need to declare the global object and do the mapping in the for loop that you are doing.
also your approach is a bit wrong in terms of when you iterate through for aren't you overwriting ?
say you have two documents where pusername is abdul.
so in your case you loose first object which will get overwritten by the second one.
i see that you commented out a return statement but even that wont work properly.
from a design point of view your approach is not efficient.
in mongoose you can do:
{
var userProjectSchema = new mongoose.Schema({
createdBy: { type: String }
, proName: String
, proType: String
, proDesc: String
});
// Find a single document by username.
userProjectSchema.findOne({ pusername : 'abdul' }, function(err, resDoc) {
if (err) return console.error(err);
// do your html stuff here
});
// Find all documents.
userProjectSchema.find(function(err, results) {
if (err) return console.error(err);
// do your html stuff here
});
}

How to query from within Mongoose pre hook in a Node.js / Express app?

I'm building a basic blog in Node.js / Express using MongoDB w/ Mongoose ORM.
I have a pre 'save' hook that I'd like to use to auto-generate a blog/idea slug for me. This works fine and well, except for the part where I want to query to see if there are any other existing posts with the same slug before continuing.
However, it appears that this does not have access to .find or .findOne() and so I keep getting an error.
What's the best way to approach this?
IdeaSchema.pre('save', function(next) {
var idea = this;
function generate_slug(text) {
return text.toLowerCase().replace(/[^\w ]+/g,'').replace(/ +/g,'-').trim();
};
idea.slug = generate_slug(idea.title);
// this has no method 'find'
this.findOne({slug: idea.slug}, function(err, doc) {
console.log(err);
console.log(doc);
});
//console.log(idea);
next();
});
Unfortunately, it's not documented very well (no mention of it in the Document.js API docs), but Documents have access to their models through the constructor field - I use it all the time for logging things from plugins, which gives me access to which model they're attached to.
module.exports = function readonly(schema, options) {
schema.pre('save', function(next) {
console.log(this.constructor.modelName + " is running the pre-save hook.");
// some other code here ...
next();
});
});
For your situation, you should be able to do:
IdeaSchema.pre('save', function(next) {
var idea = this;
function generate_slug(text) {
return text.toLowerCase().replace(/[^\w ]+/g,'').replace(/ +/g,'-').trim();
};
idea.slug = generate_slug(idea.title);
// this now works
this.constructor.findOne({slug: idea.slug}, function(err, doc) {
console.log(err);
console.log(doc);
next(err, doc);
});
//console.log(idea);
});
In this you have got the document, not the model. Method findOne is not present on the document.
If you need the model, you can always retrieve it as is shown here. But more clever would be to just assign the model to a variable at the point of creation.
Then use this variable anywhere you desire. If it is in another file, then use module.exports and require to get it anywhere else in your project.
Something like this:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/dbname', function (err) {
// if we failed to connect, abort
if (err) throw err;
var IdeaSchema = Schema({
...
});
var IdeaModel = mongoose.model('Idea', IdeaSchema);
IdeaSchema.pre('save', function(next) {
var idea = this;
function generate_slug(text) {
return text.toLowerCase().replace(/[^\w ]+/g,'').replace(/ +/g,'-').trim();
};
idea.slug = generate_slug(idea.title);
// this has no method 'find'
IdeaModel.findOne({slug: idea.slug}, function(err, doc) {
console.log(err);
console.log(doc);
});
//console.log(idea);
next();
});
// we connected ok
})

Simple mongoose example not working

I am trying to run a simple mongoose/node example which I found here on stackoverflow:
var mongoose = require('mongoose'),
db = mongoose.connect('mongodb://localhost/db'),
Schema = mongoose.Schema;
var sentinel = setTimeout(function(){
throw "failed to connect to MongoDB after one minute!";
}, 60*1000); // 60 seconds
mongoose.model('User', new Schema({
properties: {
name : { type: String, index: true }
}
}));
var User = db.model('User');
var u = new User();
u.name = 'Foo';
u.save();
User.find().all(function(arr) {
clearTimeout(sentinel); // cancel the timeout sentinel
console.log('Users found');
console.log(arr);
console.log('length='+arr.length);
});
process.stdin.resume();
If I get the code right there should be an output in the terminal at the end of the script, where the message "Users found" and all users from the collection should be printed. But I just get the timeout message. Why that?
I am running my server on an Amazon EC2 micro instance. Node, Mongodb and mongoose are installed and a Mongodb server is running (I can interact with it from the terminal via "mongo"). I have also created the directory /data/db.
I don't know about mongoose but u.save() might be asynchronous because it writes to the DB. Try
u.save(function (err){
if(err) console.log(err);
User.find().all(function(arr) {
clearTimeout(sentinel); // cancel the timeout sentinel
console.log('Users found');
console.log(arr);
console.log('length='+arr.length);
});
});
Edit: This works fine
var mongoose = require('mongoose');
var connection = mongoose.connect('mongodb://localhost/my_database');
var Schema = mongoose.Schema
var User = new Schema({
author : String
, type : String
});
var MyUserModel = mongoose.model('User', User); //create and access the model User
var u = new MyUserModel();
u.author = 'authorname';
u.save(function(err){
if (err) console.log(err);
});
MyUserModel.find({}, function (err,docs) {
console.log(docs);
});
I handled this problem by adding one additional step in each router where I use DB.
It's a little bit messy but it works and 100% no leaks.
Something like this:
// file: 'routes/api/v0/users.js'
router
var User = require('../../../models/user').User,
rest = require('../../../controllers/api/v0/rest')(User),
checkDB = require('../../../middleware/checkDB');
module.exports = function (app) {
app.get('/api/v0/users', checkDB, rest.get);
app.get('/api/v0/users/:id', checkDB, rest.getById);
app.post('/api/v0/users', checkDB, rest.post);
app.delete('/api/v0/users', checkDB, rest.deleteById);
app.put('/api/v0/users', checkDB, rest.putById);
};
// file: 'middleware/checkDB.js'
var HttpError = require('../error').HttpError,
mongoose = require('../lib/mongoose');
// method which checks is DB ready for work or not
module.exports = function(req, res, next) {
if (mongoose.connection.readyState !== 1) {
return next(new HttpError(500, "DataBase disconnected"));
}
next();
};
PS If you know solution better, please let me know.

How do I get the objectID after I save an object in Mongoose?

var n = new Chat();
n.name = "chat room";
n.save(function(){
//console.log(THE OBJECT ID that I just saved);
});
I want to console.log the object id of the object I just saved. How do I do that in Mongoose?
This just worked for me:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/lol', function(err) {
if (err) { console.log(err) }
});
var ChatSchema = new Schema({
name: String
});
mongoose.model('Chat', ChatSchema);
var Chat = mongoose.model('Chat');
var n = new Chat();
n.name = "chat room";
n.save(function(err,room) {
console.log(room.id);
});
$ node test.js
4e3444818cde747f02000001
$
I'm on mongoose 1.7.2 and this works just fine, just ran it again to be sure.
Mongo sends the complete document as a callbackobject so you can simply get it from there only.
for example
n.save(function(err,room){
var newRoomId = room._id;
});
You can manually generate the _id then you don't have to worry about pulling it back out later.
var mongoose = require('mongoose');
var myId = mongoose.Types.ObjectId();
// then set it manually when you create your object
_id: myId
// then use the variable wherever
You can get the object id in Mongoose right after creating a new object instance without having to save it to the database.
I'm using this code work in mongoose 4. You can try it in other versions.
var n = new Chat();
var _id = n._id;
or
n.save((function (_id) {
return function () {
console.log(_id);
// your save callback code in here
};
})(n._id));
Other answers have mentioned adding a callback, I prefer to use .then()
n.name = "chat room";
n.save()
.then(chatRoom => console.log(chatRoom._id));
example from the docs:.
var gnr = new Band({
name: "Guns N' Roses",
members: ['Axl', 'Slash']
});
var promise = gnr.save();
assert.ok(promise instanceof Promise);
promise.then(function (doc) {
assert.equal(doc.name, "Guns N' Roses");
});
Well, I have this:
TryThisSchema.post("save", function(next) {
console.log(this._id);
});
Notice the "post" in the first line. With my version of Mongoose, I have no trouble getting the _id value after the data is saved.
With save all you just need to do is:
n.save((err, room) => {
if (err) return `Error occurred while saving ${err}`;
const { _id } = room;
console.log(`New room id: ${_id}`);
return room;
});
Just in case someone is wondering how to get the same result using create:
const array = [{ type: 'jelly bean' }, { type: 'snickers' }];
Candy.create(array, (err, candies) => {
if (err) // ...
const [jellybean, snickers] = candies;
const jellybeadId = jellybean._id;
const snickersId = snickers._id;
// ...
});
Check out the official doc
Actually the ID should already be there when instantiating the object
var n = new Chat();
console.log(n._id) // => 4e7819d26f29f407b0... -> ID is already allocated
Check this answer here: https://stackoverflow.com/a/7480248/318380
As per Mongoose v5.x documentation:
The save() method returns a promise. If save() succeeds, the
promise resolves to the document that was saved.
Using that, something like this will also work:
let id;
n.save().then(savedDoc => {
id = savedDoc.id;
});
using async function
router.post('/create-new-chat', async (req, res) => {
const chat = new Chat({ name : 'chat room' });
try {
await chat.save();
console.log(chat._id);
}catch (e) {
console.log(e)
}
});

Resources