gpt4 book ai didi

node.js - MongoDB 返回空数组

转载 作者:太空宇宙 更新时间:2023-11-03 23:09:37 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

关于node.js - MongoDB 返回空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20395083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com