node-restful icon indicating copy to clipboard operation
node-restful copied to clipboard

How to pass a Mongoose ObjectId to a previously schema called?

Open jovi-tsx opened this issue 6 years ago • 0 comments

Well, i'll try to be very clear since my english is not that good though. LOL.

I'm trying to create a person to create a user. My problem is: I have to instantiate my UserSchema to create NodeRestful Model and so the methods (get, post, etc.), but my UserSchema has a default value who receives by default a person objectId, 'cause I cannot have a user without have a person, right?

But, I only create a person right before user post action. BUT HOW I'LL INSTANTIATE USER WITHOUT HAVING A PERSON ID? I'M STUCK.

Check my code and try to understand yourselves.

routes.js

const express = require('express')

module.exports = function(server) {
    const router = express.Router()
    server.use('/api', router)

    const Board = require('../api/board/board-service')
    Board.register(router, '/boards')

    const User = require('../api/person/user/user-service')
    User.register(router, '/users')
}

user-service.js

const User = require('./user')
const errorHandler = require('../../common/errorHandler')

User.methods(['get', 'post', 'put'])
User.updateOptions({new: true, runValidators: true})

User.before('post', (req, res, next) => {
    const Person = require('../person')
    const newPerson = new Person({ name: req.body.name, email: req.body.email })
    
    newPerson.save(err => {
        if(err) errorHandler(err)
    })

    req.body.personId = newPerson._id
    next()
})

module.exports = User

user.js

const restful = require('node-restful')
const mongoose = restful.mongoose
const ObjectId = mongoose.Schema.Types.ObjectId
const bcrypt = require('bcrypt')

const utils = require('../../../utils')

const BCRYPT_WORK_FACTOR = 10

const userSchema = new mongoose.Schema({
    personId: { type: ObjectId, required: true, default: **req.body.personId** },
    initials: { type: String, required: true, default: utils.getInitials() },
    username: { type: String, required: true, unique: true, dropDups: true },
    password: { type: String, required: true }
})

userSchema.pre('save', function(next) {
    var user = this

    if(!user.isModified('password')) return next();
    
    bcrypt.genSalt(BCRYPT_WORK_FACTOR, function(err, salt) {
        if(err) return next(err);

        bcrypt.hash(user.password, salt, function(err, hash) {
            if(err) return next(err)

            user.password = hash
            next()
        })
    })
})

userSchema.methods.comparePassword = function(candidatePassword, cb) {
    bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
        if(err) return cb(err)
        cb(null, isMatch)
    })
}

module.exports = restful.model('User', userSchema)

person.js

const restful = require('node-restful')
const mongoose = restful.mongoose

const personSchema = new mongoose.Schema({
    name: { type: String, required: true },
    email: { type: String, required: true, unique: true, dropDups: true },
    biography: { type: String }
})

module.exports = mongoose.model('Person', personSchema)

Please guys, help me, i'm very lost.

jovi-tsx avatar Aug 28 '19 01:08 jovi-tsx