Запрос Mongodb по имени отображает неверные данные - PullRequest
1 голос
/ 14 января 2020

Я хочу установить GET по запросу имени, но вместо того, чтобы получить нужную запись, я получаю последнюю в списке

Вот мои данные в пн go:

{
      number: 1,
      _id: "5df7f816d64d8dcd8e48bd48",
      name: "INFRA-",
      author: "Rebecca Topakian",
      format: "A4",
      pages: 40,
      paper: "Papier Serio Nero",
      printing: "Impression off set argent HUV",
      copies: 600,
      price: 20,
      desc_fr: "A la recherche de corps en transe, dans un état d'abandon total à la danse et au simple plaisir d'être, j'ai cherché à agir en chasseur invisible. Pour cela, j'ai transformé un appareil numérique afin qu'il ne soit sensible qu'aux longueurs d’onde correspondant à l’infrarouge. J'utilise un éclairage du même type, agissant comme un flash invisible à l'œil humain. Les sujets se révèlent alors dans des moments d’abandon, loin d’une quelconque mise en scène, et offrent au regardeur des portraits de l’oubli de soi.",
      desc_en: "In the series “Infra-”, I’m using a digital transformed camera – the low-pass filter on the sensor has been removed so the camera is sensible to infrared. In the complete night of dancing parties, I enlighten my subjects by using an infrared led panel, invisible to the human eye but visible to the camera, allowing me to act discreetly. The subjects then reveal themselves in moments of abandonment, far from any staging, and offer the viewer portraits of selflessness - «non-portraits».",
      new: false
      },

и вот мой запрос:

router.get('/books/:name', (req, res, next) => {
    let name = req.params.name;
    let value = req.params.value;
    var query = {};
query[name] = value;
    console.log(req.params);
    const localdb = db.client.db(process.env.DB_NAME);
    const collection = localdb.collection(process.env.COLL_BOOKS);
    collection.findOne(query, function (err, docs) {
        if (err) throw err
        res.status(200).send(docs);
    });
});

Спасибо

1 Ответ

0 голосов
/ 14 января 2020

Полагаю, имя attibute не является переменной, попробуйте следующее:

router.get('/books/:name', (req, res, next) => {
    let name = req.params.name;
    var query = {};
    query['name'] = name;
    const localdb = db.client.db(process.env.DB_NAME);
    const collection = localdb.collection(process.env.COLL_BOOKS);
    collection.findOne(query, function (err, docs) {
        if (err) throw err
        res.status(200).send(docs);
    });
});

вам нужно запросить mon go примерно так:

{
    name: 'TOTO'
}
...