REST API通过引用与另一个集合有关系的objectId来更新集合中的对象

时间:2018-12-02 18:04:03

标签: node.js mongodb api express mongoose

根据我在Udemy课程中所学到的所有知识,我一直在尝试着自己的一个项目,该项目涉及创建许多API,并且集合之间将保持某种联系。

例如,我有以下officeSchemauserSchemaPOST方法。当用户决定更换办公室时,我想在更新office_id对象时传递一个 new user。如何使用 new user对象以及可能需要同时更新的其他字段来更新office_address对象? (鉴于我正在使用先查询office_address然后将其写入用户对象的方法。)

我想知道在主体中传递 UPDATEoffice_id用户对象的更好方法。 (如果我做错了什么或可以做得更好,请随时纠正我。)

如果有人能对此提供反馈,我将非常感谢。

officeSchema

const officeSchema = new mongoose.Schema({
    address_1: {
        type: String,
        required: true,
        minlength: 2,
        maxlength: 50,
        trim: true
    },
    address_2: {
        type: String,
        required: true,
        minlength: 2,
        maxlength: 50,
        trim: true
    },
    city: {
        type: String,
        required: true,
        minlength: 2,
        maxlength: 50,
        trim: true
    },
    state: {
        type: String,
        required: true,
        minlength: 2,
        maxlength: 50,
        trim: true
    },
    zip: {
        type: Number,
        required: true,
        minlength: 5,
        maxlength: 10,
        trim: true
    },
    manager: {
        type: String,
        required: true,
        minlength: 2,
        maxlength: 50
    },
    phone: {
        type: Number,
        required: true,
        minlength: 10,
        maxlength: 14
    },
    notes: {
        type: String,
        minlength: 2,
        maxlength: 1000
    },
    updated_at: {
        type: Date,
        default: Date.now
    },
    last_updated_by: {
        type: String
    }
});

userSchema

const userSchema = new mongoose.Schema({
    name: {
        type: String
    },
    first_name: {
        type: String,
        required: true,
        minlength: 2,
        maxlength: 50,
        lowercase: true,
        trim: true
    },
    last_name: {
        type: String,
        required: true,
        minlength: 2,
        maxlength: 50,
        lowercase: true,
        trim: true
    },
    username: {
        type: String,
        required: true,
        minlength: 2,
        maxlength: 50,
        lowercase: true,
        trim: true,
        unique: true
    },
    email: {
        type: String,
        required: true,
        minlength: 2,
        maxlength: 50,
        lowercase: true,
        trim: true,
        unique: true
    },
    password: {
        type: String,
        required: true,
        minlength: 6,
        maxlength: 1024,
        trim: true
    },
    office_address: {
        type: officeSchema,
        required: true
    },
    phone: {
        type: Number,
        minlength: 10,
        maxlength: 14,
    },
    ext: {
        type: Number,
        minlength: 4,
        maxlength: 6
    },
    role: {
        type: String,
        enum: ['super admin', 'admin', 'user'],
        default: 'user',
        required: true,
    },
    pc_name: {
        type: String,
        minlength: 2,
        maxlength: 50,
        trim: true
    },
    updated_at: {
        type: Date,
        default: Date.now
    },
    updated_by: {
        type: String,
        required: true
    },
    created_by: {
        type: String,
        required: true,
    },
    notes: {
        type: String,
        minlength: 2,
        maxlength: 1000
    },
    exempt: {
        type: Boolean,
        required: true,
        default: false
    },
    remote_user: {
        type: Boolean,
        required: true,
        default: false
    },
    enabled: {
        type: Boolean,
        required: true,
        default: true
    }
});

POST用户

router.post('/', [auth, isAdmin], asyncMiddleware(async (req, res) => {
// Validate info in the request body
const { error } = validate(req.body);
// if request body info doesn't match schema then send 400 response
if(error) return error400(res, error);

// find location by id to populate user object with location information
const office = await Office.findById(req.body.office_id);

// query db to find out if user already exists
let user = await User.findOne({ email: req.body.email });

// if user found then throw 400 response
if (user) return res.status(400).json({
    message: 'User already registerd!'
})

// Create new user
user = new User({
    name: req.body.name,
    first_name: req.body.first_name,
    last_name: req.body.last_name,
    username: req.body.username,
    email: req.body.email,
    password: req.body.password,
    current_password: req.body.current_password,
    last_password: req.body.last_password,
    office_address: {
        _id: office._id,
        address_1: office.address_1,
        address_2: office.address_2,
        city: office.city,
        state: office.state,
        zip: office.zip,
        manager: office.manager,
        phone: office.phone
    },
    home_address: {
        address_1: req.body.home_address.address_1,
        address_2: req.body.home_address.address_2,
        city: req.body.home_address.city,
        state: req.body.home_address.state,
        zip: req.body.home_address.zip
    },
    phone: req.body.phone,
    ext: req.body.ext,
    role: req.body.role,
    pc_name: req.body.pc_name,
    pan_start: req.body.pan_start,
    pan_term: req.body.pan_term,
    created_by: req.user.name,
    notes: req.body.notes,
    exempt: req.body.exempt,
    remote_user: req.body.remote_user,
    enabled: req.body.enabled
})

// Use bcrypt to hash passwords
const salt = await bcrypt.genSalt(10);
user.password = await bcrypt.hash(user.password, salt);

// Save user to DB
await user.save();

谢谢!

0 个答案:

没有答案