gpt4 book ai didi

node.js - 如何使用 NodeJS 与 mongo Db 中的用户和给定记录关联

转载 作者:太空宇宙 更新时间:2023-11-04 01:44:52 25 4
gpt4 key购买 nike

我是 NodeJs 的新手,我在 Node 中有一个使用 Mongo Db 作为数据库的后端。我可以在后端执行 CRUD 操作,问题是当我对所有电影执行 GET 时,这就是响应

{
"status": "success",
"message": "Movies list found!!!",
"data": {
"movies": [
{
"id": "5b72b2663911d82018cb81c3",
"name": "Iron Man",
"released_on": "2018-05-12T00:00:00.000Z"
}
]
}
}

在上面的例子中,我不知道哪个用户创建了这部电影,所以我想要这样的东西:

 {
"status": "success",
"message": "Movies list found!!!",
"data": {
"movies": [
{
"id": "5b72b2663911d82018cb81c3",
"name": "Iron Man",
"released_on": "2018-05-12T00:00:00.000Z"
"creating_user" : [
//informations regarding the user who created the movie in the db
]
}
]
}
}

为此,我有以下模型:

用户模型

const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const saltRounds = 10;

//Define a schema
const Schema = mongoose.Schema;

const UserSchema = new Schema({
name: {
type: String,
trim: true,
required: true,
},
email: {
type: String,
trim: true,
required: true
},
password: {
type: String,
trim: true,
required: true
}
});
// hash user password before saving into database
UserSchema.pre('save', function (next) {
this.password = bcrypt.hashSync(this.password, saltRounds);
next();
});
module.exports = mongoose.model('User', UserSchema);

电影模特

const User = require("../models/users")
const mongoose = require('mongoose');
//Define a schema
const Schema = mongoose.Schema;

const MovieSchema = new Schema({
name: {
type: String,
trim: true,
required: true,
},
released_on: {
type: Date,
trim: true,
required: true
},
creating_user:[{type: Schema.Types.ObjectId, ref: 'User'}]
});
module.exports = mongoose.model('Movie', MovieSchema)

这是我的 Controller

const movieModel = require('../models/movies');
const usersModel = require('../models/users');
module.exports = {

create: function (req, res, next) {
movieModel.create({ name: req.body.name, released_on: req.body.released_on, creating_user: "DONT KNOW WHAT TO PUT HERE" }, function (err, result) {
if (err)
next(err);
else
res.json({ status: "success", message: "Movie added successfully!!!", data: null });

});
},
}

总而言之,我的问题是,我应该在 Controller 中放入什么,以便将用户信息与电影信息一起保存。

最佳答案

您需要在创建电影时将 userId(用户的 mongo _id)放入create_user 字段中。

const movieModel = require('../models/movies');
const usersModel = require('../models/users');
module.exports = {

create: function (req, res, next) {
let userId=//get userId from req or auth token if you are using jwt
movieModel.create({ name: req.body.name, released_on:
req.body.released_on, creating_user: userId }, function (err, result) {
if (err)
next(err);
else
res.json({ status: "success", message: "Movie added successfully!!!", data: null });

});
},
}

当您获得所有电影列表时,您将必须填充create_user,以便您可以获得用户记录:

  movieModel.find({}).populate('creating_user')
.exec(function (err, item) {
//do your stuff here
});

关于node.js - 如何使用 NodeJS 与 mongo Db 中的用户和给定记录关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51840269/

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