节点 js REST 服务 mongodb 不同的数组为空
Node js REST Service mongodb distinct array empty
我是一名初级开发人员,我正在 Azure 上使用 Node JS、Express、MongoDB 开发 REST 服务。
在我的数据库上,我有更多机器的集合,其中包含模型、类型等字段,当我执行不同的查询时,我收到一个字符串数组作为响应。
示例:
[
"Massey Ferguson",
"JOHN DEERE",
"FENDT",
"NEW HOLLAND",
"CASE-IH",
"DEUTZ-FAHR",
"VALTRA/VALMET",
"SAME",
"BCMH",
"CASE",
"CATERPILLAR",
"CHALLENGER"
]
现在我写了一个GET服务:
router.get('/machine_brand', function(req,res){
Machine.find().distinct('Brand', (err, items) => {
if (err) {
console.log(err);
return res.status(400).send({ status: 'ko', data: {msg: err.message }});
console.log(err);
}
console.log(items);
res.status(200).send({status: 'ok', data: {msg: 'Brands available', brand:items}});
});
});
服务响应是 200 但作为 json 响应我收到一个空数组
{
"status": "ok",
"data": {
"msg": "Brands available",
"brand": []
}
}
为什么数组是空的?
如何将我在 robomongo 上 运行 的 mongo 查询的相同响应放入数组中?
我也尝试过使用 find() 并且 ruslt 是相同的:一个空数组 []
router.get('/machine', function(req,res){
Machine.find({}, (err, machine) => {
if (err) {
console.log(err);
return res.status(400).send({ status: 'ko', data: {msg: err.message }});
}
res.status(200).send({status: 'ok', data: {msg: 'Lista machine', machine: machine}});
});
});
这是我的模型:
var db = require('../config/db.js');
var MachineSchema = db.Schema({
Brand: {type: String, required: true},
Model: {type: Number, required: true},
Version:{type: String, required: true},
Hp: {type: String, required: true}
});
db.model('Machine', MachineSchema);
module.exports = db.model('Machine');
感谢您的帮助
最好的,
这看起来应该可行,而且您收到回复这一事实是一个好兆头。我的第一个建议是确保集合名称与您的数据库正确匹配。我刚刚测试了一段代码(获取名称为 'UsErS' 的集合),似乎 MongoDB 在这种情况下不会抛出错误,而只是使用提供的名称创建一个新的空集合。
编辑:如果您使用不同的库获得填充的响应,那么还要仔细检查您的库的初始化以确保它命中正确的数据库等。
我是一名初级开发人员,我正在 Azure 上使用 Node JS、Express、MongoDB 开发 REST 服务。 在我的数据库上,我有更多机器的集合,其中包含模型、类型等字段,当我执行不同的查询时,我收到一个字符串数组作为响应。 示例:
[
"Massey Ferguson",
"JOHN DEERE",
"FENDT",
"NEW HOLLAND",
"CASE-IH",
"DEUTZ-FAHR",
"VALTRA/VALMET",
"SAME",
"BCMH",
"CASE",
"CATERPILLAR",
"CHALLENGER"
]
现在我写了一个GET服务:
router.get('/machine_brand', function(req,res){
Machine.find().distinct('Brand', (err, items) => {
if (err) {
console.log(err);
return res.status(400).send({ status: 'ko', data: {msg: err.message }});
console.log(err);
}
console.log(items);
res.status(200).send({status: 'ok', data: {msg: 'Brands available', brand:items}});
});
});
服务响应是 200 但作为 json 响应我收到一个空数组
{
"status": "ok",
"data": {
"msg": "Brands available",
"brand": []
}
}
为什么数组是空的? 如何将我在 robomongo 上 运行 的 mongo 查询的相同响应放入数组中? 我也尝试过使用 find() 并且 ruslt 是相同的:一个空数组 []
router.get('/machine', function(req,res){
Machine.find({}, (err, machine) => {
if (err) {
console.log(err);
return res.status(400).send({ status: 'ko', data: {msg: err.message }});
}
res.status(200).send({status: 'ok', data: {msg: 'Lista machine', machine: machine}});
});
}); 这是我的模型:
var db = require('../config/db.js');
var MachineSchema = db.Schema({
Brand: {type: String, required: true},
Model: {type: Number, required: true},
Version:{type: String, required: true},
Hp: {type: String, required: true}
});
db.model('Machine', MachineSchema);
module.exports = db.model('Machine');
感谢您的帮助 最好的,
这看起来应该可行,而且您收到回复这一事实是一个好兆头。我的第一个建议是确保集合名称与您的数据库正确匹配。我刚刚测试了一段代码(获取名称为 'UsErS' 的集合),似乎 MongoDB 在这种情况下不会抛出错误,而只是使用提供的名称创建一个新的空集合。
编辑:如果您使用不同的库获得填充的响应,那么还要仔细检查您的库的初始化以确保它命中正确的数据库等。