Mongoose + node.js findOne(),find()没有给出结果

时间:2014-11-19 13:51:38

标签: node.js mongodb mongoose

我几天前就开始学习node.js,我在检索数据时遇到了一些问题。

我在node.js中使用mongoose并且save()函数正在工作但是当我尝试使用find()或findOne()检索数据时,我的页面会长时间加载并且不会给出任何结果。

模型/ chatrooms.js

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

var tblchatroomsSchema = new Schema({
    chatusers: { type: Schema.ObjectId, ref: 'tblchatrooms' },
    chatroomid: {type: Number, required: true, index: { unique: true }},        // Every chat has 1 room id
    totalmember: Number,    // Indicates total number of members
    status: Number,         // 0 = closed, 1 = Open
    created: Date,
    chatlog: String
});

module.exports = mongoose.model('tblchatrooms', tblchatroomsSchema);

routes.js

>     var tblchatrooms = require('./models/tblchatrooms');
>     ......
>     ......
>     
>         app.get('/member', function (req, res) {
>             
>             //res.send("Hello World");
>             //exit;
>             if(!req.session.userid){
>                 res.send('Please login to chat with verified Membmers.');
>             }
>     
>              tblchatrooms.findOne({totalmember: 1, status: 1}), function(err, c) {
>                 if(err){
>                     res.send(err);
>                 } else {
>                     console.log(c);
>                     res.send(c);
>                 };
>             };


});

我正在通过2条条件找到一条记录。

我收藏的文件

tblchatrooms

/* 0 */
{
    "_id" : ObjectId("546c9a6685ff7dc00d7d3b72"),
    "chatusers" : ObjectId("546c58c2fe985f480c008055"),
    "chatroomid" : 517048,
    "totalmember" : 1,
    "status" : 1,
    "created" : ISODate("2014-11-19T13:25:58.410Z"),
    "chatlog" : "empty",
    "__v" : 0
}

/* 1 */
{
    "_id" : ObjectId("546c9a7f85ff7dc00d7d3b73"),
    "chatusers" : ObjectId("546c58c2fe985f480c008055"),
    "chatroomid" : 894109,
    "totalmember" : 1,
    "status" : 1,
    "created" : ISODate("2014-11-19T13:26:23.114Z"),
    "chatlog" : "empty",
    "__v" : 0
}

请帮我解决这个错误

谢谢

1 个答案:

答案 0 :(得分:0)

我猜这是一个语法错误,因为您传递回调。应该是:

tblchatrooms.findOne({totalmember: 1, status: 1}, function(err, c) {

而不是

tblchatrooms.findOne({totalmember: 1, status: 1}), function(err, c) {