我有一个页面架构,该页面架构有两个对象属性createdBy
和approvedBy
.
当我用
.populate("approvedBy").populate("createdBy");
,然后显示"approvedBy": null , "createdBy": null
,
当我不用populate()
的时候,就用find()
,然后显示ObjectId
.但是我需要所有的嵌套对象,比如使用populate().
Page Schema:个
const mongoose = require("mongoose");
const PageSchema = new mongoose.Schema({
title: {
type: String,
require: true,
},
description: {
type: String,
require: true,
},
url: {
type: String,
},
logoURL: {
type: String,
},
backgroundImageURL: {
type: String,
},
button: {
title: {
type: String,
require: true,
},
url: {
type: String,
},
color: {
type: String,
},
},
createdBy: {
type: mongoose.Types.ObjectId,
ref: "User",
require: true,
},
approvedBy: {
type: mongoose.Types.ObjectId,
ref: "User",
require: true,
},
approve: {
type: Boolean,
default: false,
},
approveDate: {
type: Date,
},
archive: {
type: Boolean,
default: false,
},
archiveDate: {
type: Date,
},
createdAt: {
type: Date,
default: Date.now,
},
updatedAt: {
type: Date,
},
});
module.exports = mongoose.model("Page", PageSchema);
by using 100code:
const pages = async (req, res, next) => {
try {
const pages = await Page.find({})
.populate("approvedBy")
.populate("createdBy");
if (pages === null || pages.length == 0) {
throw createError(404, "NO DATA FOUND");
}
return res.status(200).json({ success: 1, pages });
} catch (error) {
return next(createError(error));
}
};
by using 100 output:
{
"success": 1,
"pages": [
{
"button": {
"title": "Play Now",
"url": "btnURL",
"color": "#ff6767"
},
"_id": "6425a0d069c436a4f056ba26",
"title": "Dengen NFT 2",
"description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
"url": "imgURL",
"logoURL": "logoURL",
"backgroundImageURL": "bgURL",
"createdBy": "64231bf9f21deb779a148c64",
"approve": false,
"archive": false,
"createdAt": "2023-03-30T14:46:40.312Z",
"__v": 0,
"approvedBy": "64231b8a6c0398e50a8472d1"
}
]
}
by using 100 code:
const pages = async (req, res, next) => {
try {
const pages = await Page.find({}).populate("approvedBy", "createdBy");
if (pages === null || pages.length == 0) {
throw createError(404, "NO DATA FOUND");
}
return res.status(200).json({ success: 1, pages });
} catch (error) {
return next(createError(error));
}
};
by using 100 output:
100,
{
"success": 1,
"pages": [
{
"button": {
"title": "Play Now",
"url": "btnURL",
"color": "#ff6767"
},
"_id": "6425a0d069c436a4f056ba26",
"title": "Dengen NFT 2",
"description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
"url": "imgURL",
"logoURL": "logoURL",
"backgroundImageURL": "bgURL",
"createdBy": null,
"approve": false,
"archive": false,
"createdAt": "2023-03-30T14:46:40.312Z",
"__v": 0,
"approvedBy": null
}
]
}