简体   繁体   English

如何使用 Mongoose 将键值对附加到现有的 Mongodb 对象

[英]How to Append key: value pairs to existing Mongodb objects using Mongoose

 const express = require('express'); const bcrypt = require('bcryptjs'); const passport = require('passport'); const { ensureAuthenticated } = require('../config/auth'); const router = express.Router(); //User/Post model const User = require('../models/DBmusevista'); //Welcome/register page router.get('/', (req, res) => res.render('register')); //Login page router.get('/login', (req, res) => res.render('login')); //Home page router.get('/home', ensureAuthenticated, (req, res) => res.render('home', {user_mv: req.user})); //Create topic page router.get('/create-topic', ensureAuthenticated, (req, res) => res.render('create-topic', {user_mv: req.user})); //Register handle router.post('/', (req, res) => { const {firstname, lastname, username, email, password} = req.body; let errors =[]; if(!firstname || !lastname || !username || !email || !password) { errors.push({msg: 'Please check all fields.'}); }; //Check password length if(password.length < 6) { errors.push({msg: 'Password should be at least 6 characters.'}); }; if(errors.length > 0){ res.render('register', { errors, firstname, lastname, username, email, password }); }else { //Validation passed User.findOne({email: email}) .then(user => { if(user){ //User exists errors.push({msg: 'Email is already registered.'}) res.render('register', { errors, firstname, lastname, username, email, password }); } else { const newUser = new User({ //Check User firstname, lastname, username, email, password }); //Hash password bcrypt.genSalt(10, (err, salt) => bcrypt.hash(newUser.password,salt, (err, hash) => { if(err) throw err; //Set password to hash newUser.password = hash; //Save user newUser.save() .then(user => { req.flash('success_msg', 'You are now registered and can now log in'); res.redirect('/login'); }) .catch(err => console.log(err)); })) } }); } }); //Post handle router.post('/create-topic', (req, res) => { let newObj = { title: req.body.title, category: req.body.category, vista: req.body.vista, description: req.body.description } User.updateOne({username: user_mv.username}, { $set: {post: newObj}}); res.send('Hello'); //This is the code that does not work. Anything else I've tried has not worked. }); //Login handle router.post('/login', (req, res, next) => { passport.authenticate('local', { successRedirect: '/home', failureRedirect: '/login', failureFlash: true })(req, res, next); }); module.exports = router;
 const mongoose = require('mongoose'); const DBmusevistaSchema = mongoose.Schema({ firstname: { type: String, required: false }, lastname: { type: String, required: false }, username: { type: String, required: false }, email: { type: String, required: false }, password: { type: String, required: false }, date: { type: Date, default: Date.now } }); const DBmusevista = mongoose.model('DBmusevista', DBmusevistaSchema); module.exports = DBmusevista;

When an individual comes to my website the beginning page ('/') is a registration for for my site.当个人访问我的网站时,起始页 ('/') 是对我网站的注册。 I have a post request for my MongoDB collection that creates a 'user register' object (fname, lname, username, email, pass).我有一个针对我的 MongoDB 集合的 post 请求,它创建了一个“用户注册”对象(fname、lname、用户名、电子邮件、pass)。 Once logged in ('/home') a user can then create a new post(s) form that is located in my create-topic.ejs file('/create-topic').登录 ('/home') 后,用户可以创建位于我的 create-topic.ejs 文件 ('/create-topic') 中的新帖子表单。 I'm new to using MongoDB so my question is how can I append the post submitted (title, category, vista, description) to an array for the users' object.我是使用 MongoDB 的新手,所以我的问题是如何将提交的帖子(标题、类别、远景、描述)附加到用户对象的数组中。

I'm not sure if I should add an array (posts: []) to DBmusevistaSchema and create a Post.Schema({key: value}) in my DBmusevista file?我不确定是否应该向 DBmusevistaSchema 添加一个数组 (posts: []) 并在我的 DBmusevista 文件中创建一个 Post.Schema({key: value}) ? I tried using User.findOne but I cannot get it to work.我尝试使用 User.findOne 但我无法让它工作。 Please help.请帮忙。

try {
  const pirs = await Pir.find();
  pirs.forEach(async (pir) => {
  let pir2 = new Pir();
  pir.isSubmitted = false;
  pir2 = pir;
  const out = await pir2.save();
  console.log(out);      
  });
} catch (err) {
  next(err);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用猫鼬,如何通过表单将键值对作为对象插入数组? - Using Mongoose, how do I insert key value pairs as objects into an array via a form? append object 使用 Z758D31F351CE11BF27C522F516CB4C2Z 到 mongoDB 中的对象数组 - append object to array of objects in mongoDB using mongoose 如何将键/值对数组更新为猫鼬? - How to update array of key/value pairs into a mongoose? 猫鼬如何将键:值添加到现有对象 - Mongoose how to add a key:value to an existing object Restructure object array with key value pairs in Javascript and output or emit it to save it with mongoose and MongoDB? - Restructure object array with key value pairs in Javascript and output or emit it to save it with mongoose and MongoDB? 如何使用node.js计算mongodb文档中的键值对的数量? - How to count the number of key value pairs in a mongodb document using node.js? 使用 Mongoose 从对象数组中获取和操作键的值 - Getting and manipulating a value of a key from an array of objects using Mongoose 在Neo4j中,如何使用graphql将生成查询后获得的键值对存储到另一个现有节点中? - In Neo4j, how can we store the key value pairs obtained after generating a query into another existing node using graphql? 关于复制 javascript 对象的键值对 - Regarding copying key value pairs of javascript objects 猫鼬模式-如何创建一个模式,其中一个字段是键值对数组? - Mongoose Schema - How to create a schema where one of the fields is an array of key-value pairs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM