How to set the default property when creating a new user?
When I create a new user, I want to set the default property: avatar
this value "http://images.fineartamerica.com/images-medium-large/alien-face-.jpg". That is, when I send a form from the frontend there are properties: name, email, phonenumber, password
except for the property avatar
it should be set by default when creating a user. How to implement it?
Model:
const mongoose = require('mongoose');
const bcrypt = require('bcrypt-nodejs');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: {type:String},
email: {type:String},
phonenumber: {type:Number},
password: {type:String},
avatar: {type:String},
lastlogin: {type:Date}
});
User:
const mongoose = require( 'mongoose' );
const User = require('../models/user');
const config = require('../config');
exports.signup = function(req, res){
// Check for registration errors
const name = req.body.name;
const email = req.body.email;
const phonenumber = req.body.phonenumber;
const password = req.body.password;
const avatar = req.body.avatar;
if (!name || !email || !phonenumber || !password) {
return res.status(422).json({ success: false, message: 'Posted data is not correct or incomplete.'});
}
User.findOne({ email: email }, function(err, existingUser) {
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
// If user is not unique, return error
if (existingUser) {
return res.status(201).json({
success: false,
message: 'email already exists.'
});
}
// If no error, create account
let oUser = new User({
name: name,
email: email,
phonenumber: phonenumber,
password: password,
avatar: avatar
});
oUser.save(function(err) {
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
res.status(201).json({
success: true,
message: 'User created successfully, please login to access your account.'
});
});
});
};
exports.getuserDetails = function(req, res){
User.find({_id:req.params.id}).exec(function(err, user){
if(err){ res.status(400).json({ success: false, message: `Error processing request ${err}`}); }
res.status(201).json({
success: true,
data: user
});
});
};
exports.updateUser = function(req, res){
const name = req.body.name;
// const email = req.body.email;
const phonenumber = req.body.phonenumber;
const userid = req.params.id;
const avatar = req.body.avatar;
if (!name || !phonenumber || !userid) {
return res.status(422).json({ success: false, message: 'Posted data is not correct or incompleted.'});
} else {
User.findById(userid).exec(function(err, user){
if(err){ res.status(400).json({ success: false, message: `Error processing request ${err}`}); }
if(user){
user.name = name;
user.phonenumber = phonenumber;
user.avatar = avatar;
}
user.save(function(err){
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
res.status(201).json({
success: true,
message: 'User details updated successfully'
});
});
});
}
};
javascript node.js express registration
add a comment |
When I create a new user, I want to set the default property: avatar
this value "http://images.fineartamerica.com/images-medium-large/alien-face-.jpg". That is, when I send a form from the frontend there are properties: name, email, phonenumber, password
except for the property avatar
it should be set by default when creating a user. How to implement it?
Model:
const mongoose = require('mongoose');
const bcrypt = require('bcrypt-nodejs');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: {type:String},
email: {type:String},
phonenumber: {type:Number},
password: {type:String},
avatar: {type:String},
lastlogin: {type:Date}
});
User:
const mongoose = require( 'mongoose' );
const User = require('../models/user');
const config = require('../config');
exports.signup = function(req, res){
// Check for registration errors
const name = req.body.name;
const email = req.body.email;
const phonenumber = req.body.phonenumber;
const password = req.body.password;
const avatar = req.body.avatar;
if (!name || !email || !phonenumber || !password) {
return res.status(422).json({ success: false, message: 'Posted data is not correct or incomplete.'});
}
User.findOne({ email: email }, function(err, existingUser) {
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
// If user is not unique, return error
if (existingUser) {
return res.status(201).json({
success: false,
message: 'email already exists.'
});
}
// If no error, create account
let oUser = new User({
name: name,
email: email,
phonenumber: phonenumber,
password: password,
avatar: avatar
});
oUser.save(function(err) {
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
res.status(201).json({
success: true,
message: 'User created successfully, please login to access your account.'
});
});
});
};
exports.getuserDetails = function(req, res){
User.find({_id:req.params.id}).exec(function(err, user){
if(err){ res.status(400).json({ success: false, message: `Error processing request ${err}`}); }
res.status(201).json({
success: true,
data: user
});
});
};
exports.updateUser = function(req, res){
const name = req.body.name;
// const email = req.body.email;
const phonenumber = req.body.phonenumber;
const userid = req.params.id;
const avatar = req.body.avatar;
if (!name || !phonenumber || !userid) {
return res.status(422).json({ success: false, message: 'Posted data is not correct or incompleted.'});
} else {
User.findById(userid).exec(function(err, user){
if(err){ res.status(400).json({ success: false, message: `Error processing request ${err}`}); }
if(user){
user.name = name;
user.phonenumber = phonenumber;
user.avatar = avatar;
}
user.save(function(err){
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
res.status(201).json({
success: true,
message: 'User details updated successfully'
});
});
});
}
};
javascript node.js express registration
add a comment |
When I create a new user, I want to set the default property: avatar
this value "http://images.fineartamerica.com/images-medium-large/alien-face-.jpg". That is, when I send a form from the frontend there are properties: name, email, phonenumber, password
except for the property avatar
it should be set by default when creating a user. How to implement it?
Model:
const mongoose = require('mongoose');
const bcrypt = require('bcrypt-nodejs');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: {type:String},
email: {type:String},
phonenumber: {type:Number},
password: {type:String},
avatar: {type:String},
lastlogin: {type:Date}
});
User:
const mongoose = require( 'mongoose' );
const User = require('../models/user');
const config = require('../config');
exports.signup = function(req, res){
// Check for registration errors
const name = req.body.name;
const email = req.body.email;
const phonenumber = req.body.phonenumber;
const password = req.body.password;
const avatar = req.body.avatar;
if (!name || !email || !phonenumber || !password) {
return res.status(422).json({ success: false, message: 'Posted data is not correct or incomplete.'});
}
User.findOne({ email: email }, function(err, existingUser) {
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
// If user is not unique, return error
if (existingUser) {
return res.status(201).json({
success: false,
message: 'email already exists.'
});
}
// If no error, create account
let oUser = new User({
name: name,
email: email,
phonenumber: phonenumber,
password: password,
avatar: avatar
});
oUser.save(function(err) {
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
res.status(201).json({
success: true,
message: 'User created successfully, please login to access your account.'
});
});
});
};
exports.getuserDetails = function(req, res){
User.find({_id:req.params.id}).exec(function(err, user){
if(err){ res.status(400).json({ success: false, message: `Error processing request ${err}`}); }
res.status(201).json({
success: true,
data: user
});
});
};
exports.updateUser = function(req, res){
const name = req.body.name;
// const email = req.body.email;
const phonenumber = req.body.phonenumber;
const userid = req.params.id;
const avatar = req.body.avatar;
if (!name || !phonenumber || !userid) {
return res.status(422).json({ success: false, message: 'Posted data is not correct or incompleted.'});
} else {
User.findById(userid).exec(function(err, user){
if(err){ res.status(400).json({ success: false, message: `Error processing request ${err}`}); }
if(user){
user.name = name;
user.phonenumber = phonenumber;
user.avatar = avatar;
}
user.save(function(err){
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
res.status(201).json({
success: true,
message: 'User details updated successfully'
});
});
});
}
};
javascript node.js express registration
When I create a new user, I want to set the default property: avatar
this value "http://images.fineartamerica.com/images-medium-large/alien-face-.jpg". That is, when I send a form from the frontend there are properties: name, email, phonenumber, password
except for the property avatar
it should be set by default when creating a user. How to implement it?
Model:
const mongoose = require('mongoose');
const bcrypt = require('bcrypt-nodejs');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: {type:String},
email: {type:String},
phonenumber: {type:Number},
password: {type:String},
avatar: {type:String},
lastlogin: {type:Date}
});
User:
const mongoose = require( 'mongoose' );
const User = require('../models/user');
const config = require('../config');
exports.signup = function(req, res){
// Check for registration errors
const name = req.body.name;
const email = req.body.email;
const phonenumber = req.body.phonenumber;
const password = req.body.password;
const avatar = req.body.avatar;
if (!name || !email || !phonenumber || !password) {
return res.status(422).json({ success: false, message: 'Posted data is not correct or incomplete.'});
}
User.findOne({ email: email }, function(err, existingUser) {
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
// If user is not unique, return error
if (existingUser) {
return res.status(201).json({
success: false,
message: 'email already exists.'
});
}
// If no error, create account
let oUser = new User({
name: name,
email: email,
phonenumber: phonenumber,
password: password,
avatar: avatar
});
oUser.save(function(err) {
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
res.status(201).json({
success: true,
message: 'User created successfully, please login to access your account.'
});
});
});
};
exports.getuserDetails = function(req, res){
User.find({_id:req.params.id}).exec(function(err, user){
if(err){ res.status(400).json({ success: false, message: `Error processing request ${err}`}); }
res.status(201).json({
success: true,
data: user
});
});
};
exports.updateUser = function(req, res){
const name = req.body.name;
// const email = req.body.email;
const phonenumber = req.body.phonenumber;
const userid = req.params.id;
const avatar = req.body.avatar;
if (!name || !phonenumber || !userid) {
return res.status(422).json({ success: false, message: 'Posted data is not correct or incompleted.'});
} else {
User.findById(userid).exec(function(err, user){
if(err){ res.status(400).json({ success: false, message: `Error processing request ${err}`}); }
if(user){
user.name = name;
user.phonenumber = phonenumber;
user.avatar = avatar;
}
user.save(function(err){
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
res.status(201).json({
success: true,
message: 'User details updated successfully'
});
});
});
}
};
const mongoose = require('mongoose');
const bcrypt = require('bcrypt-nodejs');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: {type:String},
email: {type:String},
phonenumber: {type:Number},
password: {type:String},
avatar: {type:String},
lastlogin: {type:Date}
});
const mongoose = require('mongoose');
const bcrypt = require('bcrypt-nodejs');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: {type:String},
email: {type:String},
phonenumber: {type:Number},
password: {type:String},
avatar: {type:String},
lastlogin: {type:Date}
});
const mongoose = require( 'mongoose' );
const User = require('../models/user');
const config = require('../config');
exports.signup = function(req, res){
// Check for registration errors
const name = req.body.name;
const email = req.body.email;
const phonenumber = req.body.phonenumber;
const password = req.body.password;
const avatar = req.body.avatar;
if (!name || !email || !phonenumber || !password) {
return res.status(422).json({ success: false, message: 'Posted data is not correct or incomplete.'});
}
User.findOne({ email: email }, function(err, existingUser) {
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
// If user is not unique, return error
if (existingUser) {
return res.status(201).json({
success: false,
message: 'email already exists.'
});
}
// If no error, create account
let oUser = new User({
name: name,
email: email,
phonenumber: phonenumber,
password: password,
avatar: avatar
});
oUser.save(function(err) {
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
res.status(201).json({
success: true,
message: 'User created successfully, please login to access your account.'
});
});
});
};
exports.getuserDetails = function(req, res){
User.find({_id:req.params.id}).exec(function(err, user){
if(err){ res.status(400).json({ success: false, message: `Error processing request ${err}`}); }
res.status(201).json({
success: true,
data: user
});
});
};
exports.updateUser = function(req, res){
const name = req.body.name;
// const email = req.body.email;
const phonenumber = req.body.phonenumber;
const userid = req.params.id;
const avatar = req.body.avatar;
if (!name || !phonenumber || !userid) {
return res.status(422).json({ success: false, message: 'Posted data is not correct or incompleted.'});
} else {
User.findById(userid).exec(function(err, user){
if(err){ res.status(400).json({ success: false, message: `Error processing request ${err}`}); }
if(user){
user.name = name;
user.phonenumber = phonenumber;
user.avatar = avatar;
}
user.save(function(err){
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
res.status(201).json({
success: true,
message: 'User details updated successfully'
});
});
});
}
};
const mongoose = require( 'mongoose' );
const User = require('../models/user');
const config = require('../config');
exports.signup = function(req, res){
// Check for registration errors
const name = req.body.name;
const email = req.body.email;
const phonenumber = req.body.phonenumber;
const password = req.body.password;
const avatar = req.body.avatar;
if (!name || !email || !phonenumber || !password) {
return res.status(422).json({ success: false, message: 'Posted data is not correct or incomplete.'});
}
User.findOne({ email: email }, function(err, existingUser) {
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
// If user is not unique, return error
if (existingUser) {
return res.status(201).json({
success: false,
message: 'email already exists.'
});
}
// If no error, create account
let oUser = new User({
name: name,
email: email,
phonenumber: phonenumber,
password: password,
avatar: avatar
});
oUser.save(function(err) {
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
res.status(201).json({
success: true,
message: 'User created successfully, please login to access your account.'
});
});
});
};
exports.getuserDetails = function(req, res){
User.find({_id:req.params.id}).exec(function(err, user){
if(err){ res.status(400).json({ success: false, message: `Error processing request ${err}`}); }
res.status(201).json({
success: true,
data: user
});
});
};
exports.updateUser = function(req, res){
const name = req.body.name;
// const email = req.body.email;
const phonenumber = req.body.phonenumber;
const userid = req.params.id;
const avatar = req.body.avatar;
if (!name || !phonenumber || !userid) {
return res.status(422).json({ success: false, message: 'Posted data is not correct or incompleted.'});
} else {
User.findById(userid).exec(function(err, user){
if(err){ res.status(400).json({ success: false, message: `Error processing request ${err}`}); }
if(user){
user.name = name;
user.phonenumber = phonenumber;
user.avatar = avatar;
}
user.save(function(err){
if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }
res.status(201).json({
success: true,
message: 'User details updated successfully'
});
});
});
}
};
javascript node.js express registration
javascript node.js express registration
asked Nov 24 '18 at 14:14
Igor ShvetsIgor Shvets
142111
142111
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
There is a default keyword when you make mongoose models, try this.
avatar: {
type: String,
default: 'http://images.fineartamerica.com/images-medium-large/alien-face-.jpg'
}
mongoose docs on default: https://mongoosejs.com/docs/defaults.html
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459047%2fhow-to-set-the-default-property-when-creating-a-new-user%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is a default keyword when you make mongoose models, try this.
avatar: {
type: String,
default: 'http://images.fineartamerica.com/images-medium-large/alien-face-.jpg'
}
mongoose docs on default: https://mongoosejs.com/docs/defaults.html
add a comment |
There is a default keyword when you make mongoose models, try this.
avatar: {
type: String,
default: 'http://images.fineartamerica.com/images-medium-large/alien-face-.jpg'
}
mongoose docs on default: https://mongoosejs.com/docs/defaults.html
add a comment |
There is a default keyword when you make mongoose models, try this.
avatar: {
type: String,
default: 'http://images.fineartamerica.com/images-medium-large/alien-face-.jpg'
}
mongoose docs on default: https://mongoosejs.com/docs/defaults.html
There is a default keyword when you make mongoose models, try this.
avatar: {
type: String,
default: 'http://images.fineartamerica.com/images-medium-large/alien-face-.jpg'
}
mongoose docs on default: https://mongoosejs.com/docs/defaults.html
edited Dec 26 '18 at 10:26
answered Nov 24 '18 at 14:20
dankobgddankobgd
2481411
2481411
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459047%2fhow-to-set-the-default-property-when-creating-a-new-user%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown