Tuesday, 18 September 2018

Submit form from logged in user with userID node

Im very new to node, So be patient. Ive tried for 2 days to search or try.I have a user login system, the user then submits a form. I have 2 databases in MongoDB - users & form.How would i relate the form to the user? I was thinking by userID (_id)?My form: @ /models/submitconst mongoose = require('mongoose') const User = require('../models/user') const jobSchema = mongoose.Schema({ userId: { type: User.getUserById() }, link: { type: String, required: true }, command: { type: String, required: true }, createdDate: { type: Date, default: Date.now }, }); const Job = module.exports = mongoose.model('Job', jobSchema); My Route: @ /routes/indexrouter.post('/submit', (req, res) => { var link = req.body.link; var command = req.body.command; // Validate req.checkBody('link', 'Valid Link required').notEmpty(); req.checkBody('command', 'Command is required').notEmpty(); const errors = req.validationErrors(); if (errors) { res.render('index', { errors: errors }); } else { const newJob = new Job({ link: link, command: command, }); Job.createJob(newJob, function(err, job){ if (err) throw err; console.log(job); }); req.flash('success_msg', 'Job Submitted...'); res.redirect('/') }

Submitted September 19, 2018 at 12:56AM by rendsolve

No comments:

Post a Comment