Answer the question
In order to leave comments, you need to log in
How to set a default value for a user property?
It is necessary that when registering a new user, the avatar property should be by default, that is, when I created a new user and entered the application, the default picture is displayed. And if I want to change it, then I go to the profile and there, in the input, I set the url of the new avatar I need.
How to set a default url for an avatar when creating a user (for example, this one: https://images.fineartamerica.com/images-medium-la... ?
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}
});
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'
});
});
});
}
};
Answer the question
In order to leave comments, you need to log in
avatar: {
type: String,
default: 'https://images.fineartamerica.com/images-medium-large/alien-face-.jpg'
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question