MongoDB返回空数组

时间:2013-12-05 08:34:50

标签: node.js mongodb mongoose

我正在尝试使用通过params发送的userid来使用图像填充相册,但即使数据库中有数据,我最终也会得到一个空数组。

"use strict"
var express = require('express');

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

console.log('Welcome');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);

mongoose.connect('mongodb://localhost/Datab');
var db = mongoose.connection;

db.once('open', function () {
    console.log('Connected to Database');

});

//Schema for the data 

var userSchema = new mongoose.Schema({
    name: String
});
var albumSchema = new mongoose.Schema({
    userid: String,
    albumName: String,
    albumDesc: String,
    albumThumbnail: String,
    imageCount: Number,
    images: [{
        type: Schema.Types.ObjectId,
        ref: 'Image'
    }]
});

var ImageSchema = new Schema({
    album_id: {
        type: String,
        ref: 'Album'
    },
    imageName: String,
    imageDescription: String,
    imageURL: String,
    likeCount: Number,
    commentCount: Number
})


var Userdata = mongoose.model('Userdata', userSchema);
var Album = mongoose.model('Album', albumSchema);
var Image = mongoose.model('Image', ImageSchema);

//display of albums
app.get('/user/:user_id/photos', function (req, res) {
    var user_id = req.params.user_id
    Album.find({
        userid: user_id
    })
        .populate('images')
        .exec(function (err, albums) {
            if (!err) {
                return res.send({
                    status: {
                        error: 0,
                        message: "Successful"
                    },
                    data: {
                        albums: albums
                    }
                })
            } else
                res.send('Unsuccessful');
        })


});


app.listen(3000);

输出:

{

    "status": {

        "error": 0,

        "message": "Successful"

    },

    "data": {

        "albums": []

    }

}

为什么我得到一个空数组作为输出?

在mongo shell中查找返回以下结果

db.Album.find() {
    "_id": ObjectId("529eed506c7b0a09b2204203"),
    "albumDesc": "outings",
    "albumName": "Photoclics1",
    "albumThumbnail": "http: //192.168.71.250/uploads/cat1_thumb.jpg",
    "imageCount": "1",
    "images": ["529ef0016c7b0a09b2204205", "529ef0266c7b0a09b2204206"],
    "userid": "529eec5a6c7b0a09b22041ff"
}

编辑2:这是我期望获得的输出

{
"error": {
"status": 0,
"message": "Successful"
},
"data": {
"albums": [
{
"albumName": "myclicks",
"albumID": "527102fdaed86d8807000001",
"albumDescription": "PhotosIclicked",
"albumThumbnailURL": "http: //192.168.71.250/uploads/image.jpg",
"imageCount": 2,
"images": [
{
"imageID": "527102fdaed86d8807000001",
"imageName": "mycat",
"imageDescription": "billy",
"imageURL": "http: //192.168.71.250/uploads/cat.jpg",
"imageThumbnailURL": "http: //192.168.71.250/uploads/cat_thumb.jpg",
"likeCount": 21,
"commentCount": 1
},
{
"imageID": "527102fdaed86d8807000001",
"imageName": "mycat",
"imageDescription": "billy",
"imageURL": "http: //192.168.71.250/uploads/cat1.jpg",
"imageThumbnailURL": "http: //192.168.71.250/uploads/cat1_thumb.jpg",
"likeCount": 21,
"commentCount": 1
}
]
}
]
}   

1 个答案:

答案 0 :(得分:3)

存储在images数组中的标识符在控制台中显示为String。但是,images字段的架构中的定义显示为ObjectId类型。如果您将数据修复为ObjectId或更改数据类型,则应解决该问题。

您可以通过遍历集合和数组并将每个元素转换为ObjectId来修复MongoDB控制台中的数据。