node.js - 从参考猫鼬数据库模式查询时未检索数据?
问题描述
我无法找到问题。我正在使用两个模式 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”页面登录后可以看到他的活动数据
解决方案
在你的camptab路线中
您没有使用正确的格式从数据库中获取用户信息。尝试,
Campaign.findById(user)
或者
Campaign.find({_id : user})
它们都有效。在使用 id 获取数据时,我通常使用第一个。
推荐阅读
- angular - 如何为订阅回调编写单元 - Jasmine / Angular
- c++ - 错误:将“unsigned long”类型的引用绑定到“const unsigned long”类型的值删除“const”限定符
- c++ - 编写一个 C++ 程序以乱序打印字符串的字母
- powerbi - 基于列值格式化矩阵的条件度量的 DAX 语法?
- python - 在 setter 和 @property 装饰器中遇到一些麻烦和混乱,并非所有在 `__init__` 中定义的属性都已更新
- flutter - Flutter Provider 在 initState() 中返回 null
- azure - 创建 Azure Function App 时未找到 ARM 模板存储帐户
- python - 在 Django 中保存原始 SQL 查询的最佳方法
- ios - 在 M1 Macbook 上设置 react-native 开发环境
- automation - 如何在不等待空手道框架中的响应的情况下发送背靠背http post请求