У меня есть модель пользователя Mon goose с возможностью извлечения избранного. Файл Express удается создать нового пользователя. My Mon goose модель:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const passportLocalMongoose = require('passport-local-mongoose');
//Create Schema
const User = new Schema({
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true,
required: true
},
isVerified: {
type: Boolean,
default: false
},
password: {
type: String,
required: true
},
isCharged: {
type: Boolean,
default: false
},
resetPasswordToken: String,
resetPasswordExpires: Date,
fav: {
type: [{
urlSave: {
type: String
},
imgSave: {
type: String
},
marqueSave: {
type: String
},
smSave: {
type: String
},
typeSave: {
type: String
},
precisionSave: {
type: String
},
yearsSave: {
trype: String
},
coteEuSave: {
type: String,
},
coteUsdSave: {
type: String,
},
coteGbSave: {
type: String,
},
coteChfSave: {
type: String,
},
imgSave: {
type: String
}
}]
}
});
User.plugin(passportLocalMongoose);
mongoose.model('users', User);
Часть пользователей. js файл, который создает нового пользователя:
router.post('/enregistrer', (req, res) => {
let errors = [];
if (errors.length > 0) {
res.render('users/fr/register', {
errors: errors,
name: req.body.name,
email: req.body.email,
password: req.body.password,
});
}});
Но как получить эту модель в моем избранном . js файл, чтобы обновить его новыми добавленными мной избранными?
Мои избранные. js код:
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const router = express.Router();
const { ensureAuthenticated } = require('../../helpers/fr/auth');
require('dotenv').config();
//Load User Model
require('../../models/User');
const User = mongoose.model('users');
router.post('/save', ensureAuthenticated, (req, res) => {
const newFav = {
urlSave: req.body.urlSave,
imgSave: req.body.imgSave,
marqueSave: req.body.marqueSave,
smSave: req.body.urlSave,
typeSave: req.body.typeSave,
precisionSave: req.body.precisionSave,
yearsSave: req.body.yearsSave,
coteEuSave: req.body.coteEuSave,
coteUsdSave: req.body.coteUsSave,
coteGbSave: req.body.coteGbSave,
coteChfSave: req.body.coteChfSave
}
//update user and add fav
let addFav = User.findOneAndUpdate(newFav)
addFav.urlSave
console.log(req.body.urlSave)
})
router.get('/save', ensureAuthenticated, (req, res) => {
res.send('yep')
})
module.exports = router;