0

i have a api with 2 models, Users and Books and i want to be able to do a favourite book list in the users model, how can it be done?

I think you could make a list of books within the users model but I do not really know how it should be done in mongo models or what the method would be like

There are my models:

User model

const schema = new Schema({
    username: { type: String, unique: true, required: true },
    hash: { type: String, required: true },
    firstName: { type: String, required: true },
    lastName: { type: String, required: true },
    email: { type: String, required: false },
    image: { type: String, required: false },
    createdDate: { type: Date, default: Date.now }
});

Book model

const schema = new Schema({
    BookTitulo: String,
    BookSinopsis: String,
    BookISBN: String,
    BookPortada: String,
    BookGenero: String,
    BookAutor: String,
    BookPaginas: Number,
    BookPuntuacion: Number,
    BookPrecio: Number,
    updatedAt: { type: Date, default: Date.now },
});

And the user methods:

async function authenticate({ username, password }) {
    const user = await User.findOne({ username });
    if (user && bcrypt.compareSync(password, user.hash)) {
        const { hash, ...userWithoutHash } = user.toObject();
        const token = jwt.sign({ sub: user.id }, config.secret);
        return {
            ...userWithoutHash,
            token
        };
    }
}

async function getAll() {
    return await User.find().select('-hash');
}

async function getById(id) {
    return await User.findById(id).select('-hash');
}

async function create(userParam) {
    // validate
    if (await User.findOne({ username: userParam.username })) {
        throw 'Username "' + userParam.username + '" is already taken';
    }

    const user = new User(userParam);

    // hash password
    if (userParam.password) {
        user.hash = bcrypt.hashSync(userParam.password, 10);
    }

    // save user
    await user.save();
}

async function update(id, userParam) {
    const user = await User.findById(id);

    // validate
    if (!user) throw 'User not found';
    if (user.username !== userParam.username && await User.findOne({ username: userParam.username })) {
        throw 'Username "' + userParam.username + '" is already taken';
    }

    // hash password if it was entered
    if (userParam.password) {
        userParam.hash = bcrypt.hashSync(userParam.password, 10);
    }

    // copy userParam properties to user
    Object.assign(user, userParam);

    await user.save();
}

async function _delete(id) {
    await User.findByIdAndRemove(id);
}
Alejndr
  • 65
  • 1
  • 9
  • Possible duplicate of [Mongoose document references with a one-to-many relationship](https://stackoverflow.com/questions/34985846/mongoose-document-references-with-a-one-to-many-relationship) – Chirag Ravindra May 29 '19 at 15:27
  • the question linked above has almost exactly what you might be looking for. In that question, one `user` has many `reviews` and the everytime a user is queried, the associated `reviews` must be [`populate`ed](https://mongoosejs.com/docs/populate.html). – Chirag Ravindra May 29 '19 at 15:30

0 Answers0