node.js - 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));
解决方案
您正在尝试访问book._id
,但它不存在。在你的books.map()
你books
再次使用。应该是这样的:books.map(book => { ... })
。
推荐阅读
- java - Mapstruct 映射空对象的属性
- ios - 如何检测我的应用程序的 vpn 是否在 iOS 中连接?
- c# - 如何在 DynamoDB 中存储 .Net MemorySteam 值?
- javascript - 我可以在 apollo 客户端的错误链接中调用 graphql 突变吗
- authorization - fastapi swagger 不发送带有 url 的令牌
- jmeter - 为不同的操作运行线程组
- rpa - 在 Intellibot Studio 中使用 readfromexcel 组件时出错
- python-2.7 - 在 python 2.7 中相互减去两个日期时间并返回 HH:MM:SS
- ionic4 - ionic 4 ng-true-value equivalence
- node.js - 如何在 Node js 中的异步和等待函数中创建对象?