Mongoose Populate返回空数组

时间:2013-12-01 15:00:28

标签: node.js mongodb mongoose populate

我收到一个包含以下代码的空数组:

var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost', 'mytestapp');

var SurveySchema = require('../models/Survey.js').SurveySchema;
var Survey = mongoose.model('SurveySchema', SurveySchema, 'surveys');


var UserSchema = require('../models/Survey.js').User;
var User = mongoose.model('user', UserSchema, 'users');

exports.getSurveysForUser = function(User) {
    return function (req, res) {


 User
        .findOne({_id: req.params.userId})
        .populate('surveys')
        .exec(function (err, user){
            if (err) return res.json({error: err})
            else {
                var surveyList=[];
                surveyList = user.surveys;
                console.log(surveyList);
                console.log("user: "+ user);
                res.json(surveyList);

            }
        });
}};

这是控制台输出:

 [ ]

user: { __v: 2,
  _id: 52939b8c22a7efb720000003,
  email: 'a@b.de',
  password: '202cb962ac59075b964b07152d234b70',
  surveys: []
}

这些是Mongoose模型:

exports.SurveySchema = new Mongoose.Schema({
description : String,
questions : [question] });

exports.User = new Mongoose.Schema({
name : String,
email: { type: String, unique: true },
password: { type: String, required: true},
surveys :  [{type: Schema.ObjectId, ref: 'SurveySchema'}] });

顺便说一下:

我已经尝试过User.findOne(...),然后在回调中尝试了Survey.find()。似乎第二个声明甚至没有被执行。显然我对mongoose很新。我无法找到解决这个问题的方法

你有什么想法可以帮助我吗? 我在这里找不到任何有用的解决方案,但问题不应该是一个大问题。 在此先感谢,它真的让我好几天了!!

编辑:所以这是带有方法的index.js:

var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost', 'mytestapp');

var SurveySchema = require('../models/Survey.js').SurveySchema;
var Survey = mongoose.model('SurveySchema', SurveySchema, 'surveys');



var UserSchema = require('../models/Survey.js').User;
var User = mongoose.model('user', UserSchema, 'users');

//.. here are some more methods.. 

exports.getSurveysForUser = function(User) {
return function (req, res) {
 User
            .findOne({_id: req.params.userId})
            .populate('surveys')
            .exec(function (err, user){
                if (err) return res.json({error: err})
                else {
                    var surveyList=[];
                    surveyList = user.surveys;
                    console.log(surveyList);
                    console.log("user: "+ user);
                    res.json(surveyList);

                }
            });
}};

//this is the code, that saves a response to a survey

exports.addResponse = function(ResponseSet) {
    return function (req, res) {
        console.log("bin da: addResponse");
        console.log("response zu: " + req.body.surveyId);
        console.log("von user : " + req.body.userId);

        //für user speichern

        var pUser = User.findOne({_id:req.body.userId}, function (error, user) {

                // Maybe populate doesnt work, because i only push the ID?

               user.surveys.push(Mongoose.Types.ObjectId(req.body.surveyId));

               user.save();

            }
        );



        var pSurvey = Survey.findOne({_id:req.body.surveyId}, function (error, survey) {
                survey.responses.push(Mongoose.Types.ObjectId(req.params.id));
                survey.save();
            }
        );



        //responseSet speichern

        var responseSet = new ResponseSet(req.body);
        responseSet.save(function(error, responseSet) {
            if (error || !responseSet) {
                res.json({ error : error });
            } else {

                res.json(responseSet);
            }
        });
    };

 };

这是app.js,它使用了index.js:

var Mongoose = require('mongoose');
var db = Mongoose.createConnection('localhost', 'mytestapp');

var SurveySchema = require('./models/Survey.js').SurveySchema;
var Survey = db.model('surveys', SurveySchema);
var UserSchema = require('./models/Survey.js').User;
var User = db.model('user', UserSchema);

var ResponseSetSchema = require ('./models/Survey.js').responseSet;
var ResponseSet = db.model('responseSet', ResponseSetSchema);

var express = require('express')
  , routes = require('./routes')
  , http = require('http')
  , path = require('path')
  , passport = require('passport')
  , pass = require('./config/pass')

  , user_routes = require('./routes/user');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views/app');
//app.engine('html', require('ejs').renderFile);
app.use(express.static(__dirname + '/views/app'));
app.use(express.cookieParser());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({ secret: 'securedsession' }));
app.use(passport.initialize()); // Add passport initialization
app.use(passport.session()); // Add passport initialization
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.all('/secure', pass.ensureAuthenticated);


app.get('/', function (req, res)
{
    res.render('index.html');
} );

// some more code... 

app.get('/api/secure/userSurveys/:userId', routes.getSurveysForUser(User));

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

希望它有助于解决问题! 很多人提前感谢!! :)

2 个答案:

答案 0 :(得分:1)

尝试更改:

surveys: [{type: Schema.ObjectId, ref: 'SurveySchema'}] });

surveys: [{type: Schema.Types.ObjectId, ref: 'SurveySchema'}] });

另外,请确保将调查作为孩子推送到User.surveys

http://mongoosejs.com/docs/populate.html,此部分详细介绍了您的要求:

  

参考儿童

     

然而,我们可能会发现,如果我们使用aaron对象,我们就无法获得故事列表。这是因为没有任何故事对象被“推”到aaron.stories上。“

好的,找出问题花了很多时间:

exports.getSurveysForUser = function(User) {
...
};

var用户模型未正确注入范围。改为:

exports.getSurveysForUser = (function(User) {
...
})(User);

返回的函数的中间件签名不需要传递User模型,因为它们在中间件代码中被重新启动和传递。

在index.js中,更改此

app.get('/api/secure/userSurveys/:userId', routes.getSurveysForUser(User));

app.get('/api/secure/userSurveys/:userId', routes.getSurveysForUser());

我还要求您自行测试代码并尽可能多地阅读文档。此外,可以通过多种方式实现目标。随着时间和实践,你将征服他们所有。祝你好运!

答案 1 :(得分:1)

所以我找到了解决方案! 首先,似乎没有正确要求猫鼬模式。

所以在模型中,我做了mongoose.model('modelname',schemaname);对于每个模型,现在我只对index.js中的每个模型使用mongoose.model(...)。

其次我发现了一个更重要的事情:我的testuser突然没有user.surveys了!我相信几天前它已经充满了调查。因为我多次测试我的代码,并且一些调查被推送到该集合。也许我把它收集在一些测试中......我真的不记得了。所以我在mongodb控制台中手动调查并再次测试 - >成功了! user.surveys已填充!也许这个功能在昨天工作,不需要任何改变。我很抱歉,如果那是浪费时间的话。

不好的是,现在exports.addResponse(....)只保存响应,但没有将ID推送到数组user.surveys和survey.responses。这似乎是一个同步问题,我会以某种方式解决这个问题。

无论如何,谢谢你的帮助和时间!