Abhishek V
Abhishek V

Reputation: 12526

MongoDB returning empty array

I'm trying to populate albums with images using userid sent through params but i end up with an empty array even though there is data in database.

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

output:

{

    "status": {

        "error": 0,

        "message": "Successful"

    },

    "data": {

        "albums": []

    }

}

why do i get an empty array as output?

Find in the mongo shell returns the following result

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"
}

EDIT 2: This is the output that i expect to get

{
"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
}
]
}
]
}   

Upvotes: 2

Views: 3976

Answers (1)

WiredPrairie
WiredPrairie

Reputation: 59763

The identifier stored in the images array is shown as a String in the console. Yet, the definition in the schema for the images field is shown as the type ObjectId. If you either fix the data to be an ObjectId or change the data type, the issue should be resolved.

You could fix the data in the MongoDB console by iterating through the collection and the array and converting each element to an ObjectId.

Upvotes: 3

Related Questions