node.js - _id 或来自 JSON 响应节点 js 的其他字段未排除
问题描述
我想在我的 node-express 应用程序中从 JSON API 获取数据结果时排除默认 _id 或两个字段(时间和计数)中的任何一个。我尝试了几种方法,但数据仍然显示我在 GET 响应中标记为排除的字段。在下面的代码中,我使用 _Id 作为示例,但我的数据库的每个字段都存在类似的问题。以下是我发现的一些不起作用的方法:
exports.get_all_clothing = (req, res, next) => {
Clothing.find({}, {projection:{_id: 0}})
.exec()
.then(docs => {
console.log(docs);
res.status(200).json(docs);
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
}
AND
exports.get_all_clothing = (req, res, next) => {
Clothing
.find({}).project({ _id: 0 })
.exec()
.then(docs => {
console.log(docs);
res.status(200).json(docs);
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
}
AND
exports.get_all_clothing = (req, res, next) => {
Clothing.find('-_id')
.exec()
.then(docs => {
console.log(docs);
res.status(200).json(docs);
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
}
每次我不断得到结果
[ { _id: 5cb73
time: '656
count: 43,
__v: 0 },
{ _id: 5cb73
time: '155
count: 60,
__v: 0 },
{ _id: 5cb73
time: '155
count: 56,
__v: 0 }, ....]
请帮忙。
解决方案
尝试这个:
exports.get_all_tweets = (req, res, next) => {
Clothing.find({}, {_id: 0})
.exec()
.then(docs => {
console.log(docs);
res.status(200).json(docs);
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
}
推荐阅读
- apache-spark - Apache Ignite 实例作为 Spark 广播变量
- java - Java中json文件中的股票价格
- ios - 如何使用 Swift 删除 FireStore 数组中的嵌套值?
- r - 一列R中的不同日期格式
- api - 使用 BigQuery Storage API(测试版)启动和读取多个流
- android - 使用 Kotlin 导航到不同的活动
- javascript - HTML 和 JavaScript 之间的数据转换是如何工作的?
- python - 用颜色填充图像但保留 alpha(PIL 中的颜色叠加)
- node.js - 如何修复节点中的“当前未启用对实验语法“exportDefaultFrom”的支持”
- java - 安卓导入com.firebase.ui.auth.AuthUI;失败的