首页 > 解决方案 > ES6 模板字符串不适用于 MongoDB

问题描述

我正在尝试从我的 Mongo DB 数据库中获取一些数据,并在获取后尝试使用 ES6 模板字符串向其中添加一个属性并将其发送回用户。但是我看到模板字符串中的代码没有得到评估,任何人都可以帮助我。

下面是我的代码

Books.find(query)
    .then(books => {
        const bookToDisplay = books.map(books => {
            const newBook = books.toJSON();
            newBook.links = {};
            newBook.links.self = `http//${req.headers.host}/api/book${book._id}`;
            return newBook;
        })
        // res.json(books);
        res.json(bookToDisplay);
    })
    .catch(error => res.send(error));

标签: node.jsmongodb

解决方案


您正在尝试访问book._id,但它不存在。在你的books.map()books再次使用。应该是这样的:books.map(book => { ... })


推荐阅读