【发布时间】:2020-05-16 10:42:45
【问题描述】:
我无法找到问题所在。我正在使用两个模式 user 和 campaign 。我正在使用 mongoose 填充方法通过查找唯一用户 ID 来显示活动,因为我只需要显示相关用户的数据,但没有数据进入 ejs 模板,而且我的路线也没有在活动模式中填充用户模式。我找不到数据未显示的问题。人口路线正确吗?如果是,我的 ejs 模板格式是否正确?什么问题? 10多天的困惑和挣扎
我的campatb路线是这样的
router.get("/camptab", function(req, res) {
let user = req.user.id;
Campaign.find({ user })
// User.findById(req.user.id)
.populate("userId")
.exec((err, campaign) => {
if (err) {
console.log(err);
return res.status(500).send("Something went wrong");
}
res.render("camptab", { camplist: campaign });
});
});
这样存储数据
广告系列数据不可见
我尝试在两个模式中提供参考,但它确实有效,然后尝试在单个模式中使用 ref,但我仍然面临同样的问题。我无法弄清楚这个问题。我希望用户在他的“/camptab”页面登录后可以看到他的活动数据
我的 ejs 模板
<tbody class="text-center">
<%camplist.forEach(function(camp){%>
<tr>
<td><a href="campaign/<%=camp.Title%>"><%=camp.Title%> </a></td>
<td><%=camp.Description%></td>
<td> <img src="campaign/<%=camp.Banner%>" style="width:100px; height:50px;" alt=""></td>
</tr>
<%})%>
</tbody>
广告系列架构
var mongoose = require('mongoose');
var user= require ("./User")
var Schema = mongoose.Schema;
var campaignSchema = new Schema({
Title: {type: String},
Description: { type: String },
Rules: {} ,
Banner: { type: String },
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
});
module.exports = mongoose.model('Campaigns', campaignSchema);
用户架构
const bcrypt = require('bcryptjs');
const crypto = require('crypto');
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
username: String,
email: { type: String, unique: true },
password: String,
phonenumber: Number,
passwordResetToken: String,
passwordResetExpires: Date,
emailVerificationToken: String,
emailVerified: Boolean,
snapchat: String,
facebook: String,
twitter: String,
google: String,
github: String,
instagram: String,
linkedin: String,
steam: String,
quickbooks: String,
tokens: Array,
profile: {
name: String,
gender: String,
location: String,
website: String,
picture: String
}
});
/**
* Password hash middleware.
*/
userSchema.pre('save', function save(next) {
const user = this;
if (!user.isModified('password')) { return next(); }
bcrypt.genSalt(10, (err, salt) => {
if (err) { return next(err); }
bcrypt.hash(user.password, salt, (err, hash) => {
if (err) { return next(err); }
user.password = hash;
next();
});
});
});
/**
* Helper method for validating user's password.
*/
userSchema.methods.comparePassword = function comparePassword(candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
cb(err, isMatch);
});
};
/**
* Helper method for getting user's gravatar.
*/
userSchema.methods.gravatar = function gravatar(size) {
if (!size) {
size = 100;
}
if (!this.email) {
return `https://gravatar.com/avatar/?s=${size}&d=blank`;
}
const md5 = crypto.createHash('md5').update(this.email).digest('hex');
return `https://gravatar.com/avatar/${md5}?s=${size}&d=blank`;
};
const User = mongoose.model('User', userSchema);
module.exports = User;
我尝试在两个模式中提供参考,但它确实有效,然后尝试在单个模式中使用 ref,但我仍然面临同样的问题。我无法弄清楚这个问题。我希望用户在他的“/camptab”页面登录后可以看到他的活动数据
我尝试在两个模式中提供参考,但它确实有效,然后尝试在单个模式中使用 ref,但我仍然面临同样的问题。我无法弄清楚这个问题。我希望用户在他的“/camptab”页面登录后可以看到他的活动数据
【问题讨论】:
标签: node.js mongodb express mongoose mongoose-populate