Как Mon goose Заполнить не работает с массивом ObjectIds? - PullRequest
0 голосов
/ 09 апреля 2020

Я пытаюсь заполнить запрос mon goose массивом поля objectId. В другом поле с одним objectId ссылка работает нормально.

Это моя модель "Пользователь":

const User = new Schema({
    nome: {
        type: String,
        required: true
    },
    email: {
        type:String,
        required: true 
    }
})
mongoose.model('users',User)

Это моя модель "Педидо":

const Schema = mongoose.Schema

    const Pedido = new Schema({
        id: {
            type: String,
            required: true
        },
        cliente: {
            type: Schema.Types.ObjectId, 
            ref: 'clientes',
            required: true 
        },       
        fotografos: {
            type: [Schema.Types.ObjectId], 
            ref: 'users'
        }            

    })
    mongoose.model('pedidos',Pedido)

мой router:

router.get('/pedidos',async(req,res)=>{
      var query = await Pedido.find().populate('fotografos').populate('cliente').lean().exec()
      console.log(query)
      res.render("admin/pedidos",{pedidos: query})

})

Это результат в файле console.log:

[
  {
    _id: 5e8e3bd16c57021f682b8e81,
    fotografos: [],
    cliente: {
      _id: 5e8ccceea8a28146d86f4cac,
      nome: 'Neymar',
      email: 'neymar@ney.com',
      __v: 0
    },
    __v: 0
  }
]

Поле фотографии (массив) возвращается пустым. заполнены поля клиентов.

Кто-нибудь может мне помочь с этим?

1 Ответ

0 голосов
/ 09 апреля 2020

Вы можете использовать этот код ...

fotografos:[{
            type: [Schema.Types.ObjectId], 
            ref: 'users'
        }]          


**Then do Populate**
...