Node.js mongoose .findOne不是一个函数

时间:2017-05-09 22:43:13

标签: javascript node.js mongodb mongoose es6-promise

在我的应用程序中,我确实用我的猫鼬模型回复了承诺:

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型号:

'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型号:

    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);

但是当我尝试在这个返回的模型上调用.findOne()函数时,我得到以下错误:

TypeError: room.findOne is not a function

是承诺中传递的模型而不是next .then()语句中的模型吗?

4 个答案:

答案 0 :(得分:0)

好吧,正如文档所说"queries are not promises"

那里甚至还有一个findOne()例子......

将您的代码更改为

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

你可能会有更多的运气。

答案 1 :(得分:0)

您在查询中错过了 exec()方法,尝试使用并得到解决。

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

答案 2 :(得分:0)

我自己发现问题:我传递的不是模型,我可以使用查找操作,但文档,我可以执行保存选项(找不到,因为它不是模型)。

答案 3 :(得分:0)

我应该认为你在做什么很危险。多次调用查询then() 导致多个查询调用。 https://mongoosejs.com/docs/queries.html#queries-are-not-promises 另外,也不需要执行exec().then()。只需调用then()即可执行查询;使用exec()的更好方法是实际向其传递回调。