创建新用户时如何设置默认属性?

时间:2018-11-24 14:14:16

标签: javascript node.js express registration

创建新用户时,我想设置默认属性:avatar此值“ http://images.fineartamerica.com/images-medium-large/alien-face-.jpg”。也就是说,当我从前端发送表单时,有一些属性:name, email, phonenumber, password除了属性avatar之外,在创建用户时应默认设置。如何实现?

型号:

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'
                });
            });
        });
    }
};

1 个答案:

答案 0 :(得分:2)

制作猫鼬模型时,有一个默认关键字,请尝试一下。

avatar: {
  type: String,
  default: 'http://images.fineartamerica.com/images-medium-large/alien-face-.jpg' 
}

默认的猫鼬文档:https://mongoosejs.com/docs/defaults.html