gpt4 book ai didi

express - 在 EJS 中迭代文档和子文档

转载 作者:行者123 更新时间:2023-12-02 20:32:06 24 4
gpt4 key购买 nike

当我在/gifts/new 路线上发帖时,我正在创建一个子文档。这个新数据被分配给我选择并传递的用户。

在我的索引路线上我有这个:

Gift.find({}, function(err, allGifts) {
if (err) {
req.flash('error', err.message);
} else {
res.render('gifts/index', {
title: 'Review Gifts',
gifts: allGifts,
breadcrumbsName: 'Gifts'
});
}
});

回到index.ejs模板,我正在做一个简单的:<% gifts.forEach(function (user) { %>这就是我的问题所在。我需要根据_id: 586aef6ffe5cd14ab6532889找到用户这就是迭代中的礼物对象。您可能知道,有很多礼物具有不同的用户_id:

我该怎么做?当我写这篇文章时,我想到了 } else { 内部。做一个User.find{}, cb这样我就可以通过 _id 查找用户,这是尝试过但无法正常工作的。

更新

这是我每个请求的礼物架构:

const mongoose = require('mongoose');

// Schema Setup
let giftSchema = new mongoose.Schema({
username: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Gift'
},
username: 'String'
},
giftNumber: Number,
date: Date,
status: {
review: String,
accepted: {
type: String,
redeemed: Boolean
},
declined: String,
expired: String,
pending: String,
paid: String
},
giftDescription: String,
giftAmount: Number,
giftCode: String,
redeemCode: String,
passCode: String,
senderFirstName: String,
senderLastName: String,
giftMessage: String
});

module.exports = mongoose.model('Gift', giftSchema);

这是用户:

const mongoose = require('mongoose'),
passportLocalMongoose = require('passport-local-mongoose');

let UserSchema = new mongoose.Schema({
firstName: String,
lastName: String,
aliasFirstName: String,
aliasLastName: String,
username: String,
phone: String,
password: String,
isAdmin: Boolean,
addressLine1: String,
addressLine2: String,
city: String,
state: String,
zipCode: Number,
profilePic: {
type: String,
default: 'https://s.gravatar.com/avatar/0a07df079fd7a07e4cd0e5668835296c?s=80'
},
preferredPaymentMethod: {
type: String,
enum: ['', 'paypal', 'check', 'deposit'],
default: ''
},
paymentPreference: {
paypal: {
email: {
type: String,
default: ''
}
},
check: {
addressLine1: {
type: String,
default: ''
},
addressLine2: {
type: String,
default: ''
},
city: {
type: String,
default: ''
},
state: {
type: String,
default: ''
},
zipCode: {
type: Number,
default: ''
}
},
deposit: {
routingOrTransit: {
type: String,
default: ''
},
accountNumber: {
type: String,
default: ''
}
}
},
lastLoginDate: {
type: Date,
default: Date.now
}
});

UserSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model('User', UserSchema);

最佳答案

1) 修复您的 Gift 架构,请参阅 user 属性:

const 
mongoose = require('mongoose'),
Schema = mongoose.Schema,
Types = Schema.Types;

const schema = new Schema({
user: {
type: Types.ObjectId,
ref: 'User',
required: true,
index: true
},
...
});

module.exports = mongoose.model('Gift', schema);

2)更改您的数据库查询代码:

Gift
.find({})
.populate('user') // populate will join user model to .user attribute of gift item
.exec(function(err, gifts) {
if (err) {
return res.status(500).send(err.message);
}

res.render('gifts/index', {
title: 'Review Gifts',
gifts: gifts,
breadcrumbsName: 'Gifts'
});
});

如果您想要专属用户的礼物,那么:

Gift
.find({user: req.user._id})
.populate('user')
.exec(function(err, gifts) {
if (err) {
return res.status(500).send(err.message);
}

res.render('gifts/index', {
title: 'Review Gifts',
gifts: gifts,
breadcrumbsName: 'Gifts'
});
});

3) 并在 ejs 模板中获取必要的数据,如下所示:

<% gifts.forEach(function(gift) { %>
User ID: <%= gift.user._id %><br/>
Username: <%= gift.user.username %><br/>
Fullname: <%= gift.user.firstName %> <%= gift.user.lastName %><br/>
<% } %>

关于express - 在 EJS 中迭代文档和子文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41434979/

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