Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- { jobTitle: '',
- lastPosition: '',
- email: '',
- city: '',
- accountType: 'helper',
- _id: 5c77883d8db04c921db5f635,
- username: 'here2help',
- __v: 0 }
- router.put("/profile/:id", middleware.checkProfileOwnership, function(req, res){
- console.log(req.body.profile);
- Profile.findOneAndUpdate(req.params.id, req.body.profile, function(err, updatedProfile){
- if(err){
- console.log(err.message);
- res.redirect("/profile");
- } else {
- console.log(updatedProfile);
- res.redirect("/profile/" + req.params.id);
- }
- });
- });
- { accountType: 'helper',
- username: 'here2help',
- email: 'helpingU@me.com',
- city: 'New York',
- jobTitle: 'CEO',
- lastPosition: 'sales rep'}
- { jobTitle: '',
- lastPosition: '',
- email: 'helpingu@me.com',
- city: 'New York',
- accountType: 'helper',
- _id: 5c77883d8db04c921db5f635,
- username: 'here2help',
- __v: 0 }
- var mongoose = require("mongoose");
- var passportLocalMongoose = require("passport-local-mongoose");
- var profileSchema = new mongoose.Schema({
- username: String,
- complete: { type: Boolean, default: false },
- email: { type: String, default: "" },
- city: { type: String, default: "" }
- }, { discriminatorKey: 'accountType' });
- profileSchema.plugin(passportLocalMongoose);
- module.exports = mongoose.model("Profile", profileSchema);
- var Profile = require('./profile');
- var Finder = Profile.discriminator('finder', new mongoose.Schema({
- position: { type: String, default: "" },
- skills: Array
- }));
- module.exports = mongoose.model("Finder");
- var Profile = require('./profile');
- var Helper = Profile.discriminator('helper', new mongoose.Schema({
- jobTitle: { type: String, default: "" },
- lastPosition: { type: String, default: "" }
- }));
- module.exports = mongoose.model("Helper");
- Mongoose: profiles.findAndModify({
- _id: ObjectId("5c78519e61f4b69da677a87a")
- }, [], {
- '$set': {
- email: 'finder@me.com',
- city: 'New York',
- accountType: 'helper',
- username: 'User NAme', __v: 0 } }, { new: true, upsert: false, remove: false, projection: {} })
- profiles.findAndModify({
- accountType: 'helper',
- _id: ObjectId("5c78519e61f4b69da677a87a")
- }, [], {
- '$set': {
- jobTitle: 'CTO',
- email: 'finder@me.com',
- city: 'New York',
- accountType: 'helper ',
- username: 'User Name', __v: 0 } }, { new: true, upsert: false, remove: false, projection: {} })
- app.put("/profile/:id", function(req, res){
- console.log(req.body);
- if(ObjectId.isValid(req.params.id)) {
- switch(req.body.accountType) {
- case 'helper':
- schema = Helper;
- break;
- case 'finder':
- schema = Finder;
- break;
- default:
- schema = Profile;
- }
- schema.findOneAndUpdate({ _id: req.params.id }, { $set : req.body }, { new: true, upsert: false, remove: {}, fields: {} }, function(err, updatedProfile){
- if(err){
- console.log(err);
- res.json(err);
- } else {
- console.log(updatedProfile);
- res.json(updatedProfile);
- }
- });
- } else {
- res.json({ error: "Invalid ObjectId"});
- } });
- db.profile.findOneAndUpdate({ _id: req.params.id }, { $set : req.body }, { new: true, upsert: false, remove: {}, fields: {} }, function(err, updatedProfile){
- if(err) {
- res.json(err);
- } else {
- console.log(updatedProfile);
- res.json(updatedProfile);
- }
- });
- Profile.findOneAndUpdate(
- req.params.id,
- req.body.profile,
- {
- returnNewDocument: true
- },
- function(err, updatedProfile) {
- // ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement