Jakub Pastuszuk
Jakub Pastuszuk

Reputation: 1038

Node.js mongoose .findOne is not a function

In my app, I do return promise with my mongoose model:

var roomModel = require('../../../models/room').roomModel;

roomModel.findOne({ name: req.body.roomName })
    .then(
        (room) => {
            return new Promise(function(resolve, reject) {
                //if no room present, create one, if present, check password
                if (room) {

                    if (room.password === req.body.roomPassword) {
                        return resolve(room);
                    } else {
                        return reject({
                            code: 401,
                            message: 'Room password not correct'
                        });
                    }
                } else {
                    // create new room with given data
                    var newRoom = roomModel({});
                    newRoom.name = req.body.roomName;
                    newRoom.password = req.body.roomPassword;
                    //newRoom.users = [];
                    newRoom.users[0] = {
                        name: req.body.userName
                    };

                    newRoom.save()
                        .then((data) => {
                            console.log(data);
                            if (!data) {
                                return reject({
                                    code: 500,
                                    message: 'Error when saving room'
                                });
                            } else {
                                return resolve(newRoom);
                            }
                        });
                }
            });
        }
    )
    .then((room) => {
        room.findOne({ 'users.name': req.body.userName })
            .then((user) => {
                console.log(user);
            });
    })

room.js model:

'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = require('./user').userSchema;

var room = new Schema({
    name: String,
    password: String,
    users: [userSchema]
});

module.exports.roomSchema = room;
module.exports.roomModel = mongoose.model('room', room);

users.js model:

    var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var user = new Schema({
    name: String
});

module.exports.userSchema = user;
module.exports.userModel = mongoose.model('user', user);

but when I try to call .findOne() function on this returned model, I get following error:

TypeError: room.findOne is not a function

is model passed in the promise not a model in next .then() statement?

Upvotes: 0

Views: 10940

Answers (5)

lesolorzanov
lesolorzanov

Reputation: 3586

in my case it happened that no matter how I exported it it wasn't working, and I found this solution:

var {roomModel} = require('../../../models/room')

Put the import within {}

and export it normally, i do like this:

var room = mongoose.model('room',roomSchema);

module.exports.room = room;

Upvotes: 0

Dev Yego
Dev Yego

Reputation: 553

I should think what you're doing is dangerous. Calling a queries then() multiple times might lead to multiple query calls. https://mongoosejs.com/docs/queries.html#queries-are-not-promises Also, there is no need to do exec().then(). Just calling then() executes the query; a better way to use exec() is to actually pass a callback to it.

Upvotes: 0

KARTHIKEYAN.A
KARTHIKEYAN.A

Reputation: 20098

You missed out exec() method in your query, try to use and get resolved.

roomModel.find({ name: req.body.roomName }).exec().then(/* your stuff */)

Upvotes: 0

Jakub Pastuszuk
Jakub Pastuszuk

Reputation: 1038

Found problem by myself: I was passing not the model, on which I can use find operations, but document, on which I can perform save options (not find, since it's not a model).

Upvotes: 4

Paul
Paul

Reputation: 36329

Well, as the docs say "queries are not promises".

There's even a findOne() example in there...

Change your code to

roomModel.findOne({ name: req.body.roomName }).exec().then(/* your stuff */)

and you may have more luck.

Upvotes: 0

Related Questions