node.js - 显示与当前用户相关的特定数据以进行渲染
问题描述
嗨,我想问一下如何获取当前登录用户的预订数据,
模式模型分为 2 个文件(userModels 和 bookingModels)
用户模型
const crypto = require('crypto');
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const validator = require('validator');
const userSchema = new mongoose.Schema({
name: {
type: String,
required: [true, 'Please tell us your name!'],
},
email: {
type: String,
required: [true, 'Please provide your email'],
unique: true,
lowercase: true,
validate: [validator.isEmail, 'Please provide a valid email'],
},
role: {
type: String,
enum: ['user', 'admin'],
default: 'user',
},
password: {
type: String,
required: [true, 'Please provide a password'],
minlength: 8,
select: false,
},
passwordConfirm: {
type: String,
required: [true, 'Please confirm your password'],
validate: {
validator: function (el) {
return el === this.password;
},
message: 'Passwords are not the same!',
},
},
passwordChangedAt: Date,
passwordResetToken: String,
passwordResetExpires: Date,
active: {
type: Boolean,
default: true,
select: false,
},
});
const User = mongoose.model('User', userSchema);
module.exports = User;
预订模式
const bookingSchema = new mongoose.Schema(
{
corporate: {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: [true, 'Must belong to a User'],
},
thumbnail: {
type: String,
required: [true, 'Must have a link'],
validate: [validator.isURL, 'Please provide a valid URL address'],
},
{
toJSON: { virtuals: true },
toObject: { virtuals: true },
}
);
bookingSchema.pre(/^find/, function (next) {
this.populate({
path: 'corporate',
select: 'name',
});
next();
});
const Booking = mongoose.model('Booking', bookingSchema);
module.exports = Booking;
这是呈现给前端哈巴狗的服务/处理程序
const Booking = require('../models/bookingModels');
const User = require('./../models/userModels');
exports.getMyBooking = catchAsync(async (req, res, next) => {
const bookings = await Booking.find(req.user.id, {
corporate: req.body.corporate,
thumbnail: req.body.thumbnail,
});
res.status(200).render('package', {
title: 'My Package',
bookings,
});
});
我的 PUG 文件或多或少
extends base
block content
main.main
form.form.form-user-data
.form__group
label.form__label(for='corporate') Name
input#name.form__input(type='text', value=`${booking.corporate}`, required, name='name')
.form__group.ma-bt-md
label.form__label(for='thumbnail') Detail
input#email.form__input(type='text', value=`${booking.thumbnail}`, required, name='thumbnail')
我认为其他路由器没有问题,但它一直在前端页面控制台中显示为“find()的参数“过滤器”必须是一个对象”
不太确定代码又出了什么问题,因为这是我的第一个项目。
非常感谢!!
解决方案
find() 函数需要第一个参数作为对象,并且您正在传递一个字符串。
见猫鼬文档
并且由于您的 Booking 模型中有用户引用,因此要根据用户进行过滤,您可以尝试如下:
exports.getMyBooking = catchAsync(async (req, res, next) => {
const bookings = await Booking.find({
corporate: req.user.id
});
res.status(200).render("package", {
title: "My Package",
bookings,
});
});
推荐阅读
- python - ImportError:无法从“werkzeug”导入名称“FileStorage”
- python - React Django CORS 不工作:已被 CORS 策略阻止
- python - 检查熊猫数据框中的单元格是否包含列表中的元素
- sql-server - 读取大 Excel 文件时收到“存储空间不足”错误
- r - Ggplot2中最佳拟合曲线的Y坐标
- google-apps-script - 通过 API 生成的 GoogleSheet 未执行 onChange 脚本
- wordpress - Wordpress 上的 Google Place ID
- javascript - SP2013 以分钟为单位获取时差
- javascript - Instascan 选择后置摄像头
- javascript - 打开新页面传递参数而不在 URL 上显示它们