digamos que tengo dos matrices:
authors = [ { name: 'Robert Martin', id: "afa51ab0-344d-11e9-a414-719c6709cf3e", born: 1952, }, { name: 'Martin Fowler', id: "afa5b6f0-344d-11e9-a414-719c6709cf3e", born: 1963 } ] let books = [ { title: 'Clean Code', published: 2008, author: 'Robert Martin', id: "afa5b6f4-344d-11e9-a414-719c6709cf3e", genres: ['refactoring'] }, { title: 'Agile software development', published: 2002, author: 'Robert Martin', id: "afa5b6f5-344d-11e9-a414-719c6709cf3e", genres: ['agile', 'patterns', 'design'] }, { title: 'Refactoring, edition 2', published: 2018, author: 'Martin Fowler', id: "afa5de00-344d-11e9-a414-719c6709cf3e", genres: ['refactoring'] } ]const Libro = require('./controladores/libro')
const Autor = require('./controladores/autor')
usando el esquema de objeto mongoose, la propiedad de autor del objeto de libro quiere completarse con el objeto de autor completo si libro.autor === autor.nombre.
como esto:
{ title: 'Clean Code', published: 2008, author: { name: 'Robert Martin', id: "afa51ab0-344d-11e9-a414-719c6709cf3e", born: 1952, }, id: "afa5b6f4-344d-11e9-a414-719c6709cf3e", genres: ['refactoring'] },He intentado esto:
books.forEach(book => { const bookAuthor = authors.find(author => book.author === author.name).name Book.findOne({author: bookAuthor}).populate('author') }sin éxito, de alguna manera js es difícil de manipular la estructura de datos sobre la marcha que Python, siento. ¿Podrías ayudar?