mongo ref 在数据库中返回 null?
mongo ref returning null in database?
我正在使用 MERN 堆栈开发一个 React 项目。当在数据库中创建收藏夹object userFrom returns null。虽然一切都在场。在用户模型中,我没有向用户引用角色的问题。我尝试从用户那里删除对角色模型的引用并再次尝试,但我仍然遇到同样的问题。
最喜欢的模特
const mongoose = require('mongoose');
const Favourite = mongoose.model(
"Favourite",
new mongoose.Schema({
movieId : String,
movieTitle: String,
movieImg: String,
userFrom:[
{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
]
})
)
用户模型
const mongoose = require("mongoose");
const User = mongoose.model(
"User",
new mongoose.Schema({
username: String,
email: String,
password: String,
roles: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Role"
}
]
})
);
module.exports = User;
榜样
const mongoose = require("mongoose");
const Role = mongoose.model(
"Role",
new mongoose.Schema({
name: String
})
);
module.exports = Role;
favourite.routes
const express = require('express');
const router = express.Router();
const { authJwt } = require("../middlewares");
const Favourite = require("../models/favourite.model");
router.use(function(req, res, next) {
res.header(
"Access-Control-Allow-Headers",
"x-access-token, Origin, Content-Type, Accept"
);
next();
});
router.post("/favouriteNumber", [authJwt.verifyToken], (req, res) => {
Favourite.find({"movieId": req.body.movieId})
.exec((err, favourite) => {
if(err) return res.status(400).send(err)
res.status(200).json({success: true, favouriteNumber: favourite.length})
})
})
router.post("/favourited", [authJwt.verifyToken], (req, res) => {
Favourite.find({"movieId": req.body.movieId, "userFrom": req.body.userFrom})
.exec((err, favourite) => {
if(err) return res.status(400).send(err)
let result = false;
if(favourite.length !== 0) {
result = true
}
res.status(200).json({success: true, favourited: result});
})
})
router.post("/addToFavourite", [authJwt.verifyToken], (req, res) => {
const favourite = new Favourite(req.body)
favourite.save((err, doc) => {
if(err) return res.json({success: false, err})
return res.status(200).json({success: true, doc})
})
})
router.post("/removeFavorite", [authJwt.verifyToken], (req, res) => {
Favourite.findOneAndDelete({movieId: req.body.movieId, userFrom: req.body.userFrom})
.exec((err, doc) => {
if(err) return res.json({success: false, err})
return res.status(200).json({success: true, doc})
})
})
module.exports = router;
您在创建收藏夹时并未将用户添加到收藏夹。如果您使用的是 express-jwt 那么您可以使用 req.user:
轻松做到这一点
router.post("/addToFavourite", [authJwt.verifyToken], (req, res) => {
req.body.userFrom = req.user._id
const favourite = new Favourite(req.body)
favourite.save((err, doc) => {
if(err) return res.json({success: false, err})
return res.status(200).json({success: true, doc})
})
})
我正在使用 MERN 堆栈开发一个 React 项目。当在数据库中创建收藏夹object userFrom returns null。虽然一切都在场。在用户模型中,我没有向用户引用角色的问题。我尝试从用户那里删除对角色模型的引用并再次尝试,但我仍然遇到同样的问题。
最喜欢的模特
const mongoose = require('mongoose');
const Favourite = mongoose.model(
"Favourite",
new mongoose.Schema({
movieId : String,
movieTitle: String,
movieImg: String,
userFrom:[
{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
]
})
)
用户模型
const mongoose = require("mongoose");
const User = mongoose.model(
"User",
new mongoose.Schema({
username: String,
email: String,
password: String,
roles: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Role"
}
]
})
);
module.exports = User;
榜样
const mongoose = require("mongoose");
const Role = mongoose.model(
"Role",
new mongoose.Schema({
name: String
})
);
module.exports = Role;
favourite.routes
const express = require('express');
const router = express.Router();
const { authJwt } = require("../middlewares");
const Favourite = require("../models/favourite.model");
router.use(function(req, res, next) {
res.header(
"Access-Control-Allow-Headers",
"x-access-token, Origin, Content-Type, Accept"
);
next();
});
router.post("/favouriteNumber", [authJwt.verifyToken], (req, res) => {
Favourite.find({"movieId": req.body.movieId})
.exec((err, favourite) => {
if(err) return res.status(400).send(err)
res.status(200).json({success: true, favouriteNumber: favourite.length})
})
})
router.post("/favourited", [authJwt.verifyToken], (req, res) => {
Favourite.find({"movieId": req.body.movieId, "userFrom": req.body.userFrom})
.exec((err, favourite) => {
if(err) return res.status(400).send(err)
let result = false;
if(favourite.length !== 0) {
result = true
}
res.status(200).json({success: true, favourited: result});
})
})
router.post("/addToFavourite", [authJwt.verifyToken], (req, res) => {
const favourite = new Favourite(req.body)
favourite.save((err, doc) => {
if(err) return res.json({success: false, err})
return res.status(200).json({success: true, doc})
})
})
router.post("/removeFavorite", [authJwt.verifyToken], (req, res) => {
Favourite.findOneAndDelete({movieId: req.body.movieId, userFrom: req.body.userFrom})
.exec((err, doc) => {
if(err) return res.json({success: false, err})
return res.status(200).json({success: true, doc})
})
})
module.exports = router;
您在创建收藏夹时并未将用户添加到收藏夹。如果您使用的是 express-jwt 那么您可以使用 req.user:
轻松做到这一点router.post("/addToFavourite", [authJwt.verifyToken], (req, res) => {
req.body.userFrom = req.user._id
const favourite = new Favourite(req.body)
favourite.save((err, doc) => {
if(err) return res.json({success: false, err})
return res.status(200).json({success: true, doc})
})
})