node.js - 猫鼬正在查询另一个数据库
问题描述
编辑:添加集合的图像
编辑2:经过几次调试,我尝试保存一个文档并运行 find() 查询,它工作(我找到了我保存的测试文档)。我认为现在的问题在于与数据库的连接。我可能正在连接到其他地方
edit3更改了标题,因为它似乎没有指向实际问题。
经过几次调试后,我发现 mongoose 正在查询除了我在 uri 中指定的数据库之外的其他地方。当我尝试保存测试文档时,它已成功保存,并且我能够使用 find() 将其取回。但是,当我使用 find() 时,它只返回了之前保存的测试文档。所以现在我想知道这些文件保存在哪里?
要添加更多详细信息,以下是我编写的一些代码:
apiServerRoutes.js
'use strict';
module.exports = function(app) {
var apiServer = require('../controllers/apiServerControllers');
app.route('/items')
.get(apiServer.get_all_item);
app.route('/items/:itemId')
.get(apiServer.get_item);
};
apiServerControllers.js
'use strict';
var mongoose = require('mongoose');
var Item = mongoose.model('item_m');
exports.get_all_item = function(req, res) {
console.log("get_all_item() is called");
Item.find({}, function(err, item) {
if (err)
res.send(err);
res.json(item);
});
};
exports.get_item = function(req, res) {
console.log("get_item() is called");
Item.findOne({item_typeId: req.params.typeId}, '',function(err, item) {
if (err)
res.send(err);
res.json(item);
});
};
ItemSchema.js
'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ItemSchema = mongoose.Schema({
item_name: String,
item_desc: String,
item_type: String,
});
var Item = mongoose.model('item_m', ItemSchema, 'item_m');
module.exports = Item;
服务器.js
var express = require('express'),
app = express(),
port = process.env.PORT || 3000,
mongoose = require('mongoose'),
Item = require('./api/models/ItemSchema'),
bodyParser = require('body-parser');
mongoose.Promise = global.Promise;
mongoose.set('debug', true);
mongoose.connect('mongodb+srv://[[user]]:[[pass]]@market-test-app-1dza9.mongodb.net/test?retryWrites=true&w=majority', {
useNewUrlParser: true, useUnifiedTopology: true});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("Connection Successful!");
});
var routes = require('./api/routes/apiServerRoutes');
routes(app);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.listen(port);
console.log('RESTful API server started on: ' + port);
编辑结束3
下面的东西来自我在进行一些挖掘和调试之前遇到的原始问题
所以我有这个控制器功能,它使用没有投影参数的简单查找查询
exports.get_all_item = function(req, res) {
Item.find({}, function(err, item) {
if (err)
res.send(err);
res.json(item);
});
};
每次我在邮递员中运行我的请求时,我都会得到一个空结果。当我在 express 中打开调试模式时,我看到了 mongoose 发送的这个查询
Mongoose: item_m.find({}, { projection: {} })
当我尝试在 mongodb 中运行此查询时,出现此错误
"message" : ">1 field in obj: {}",
如果我确实添加了投影参数,例如。item_name,猫鼬发送这个:
Mongoose: item_m.find({}, { projection: { item_name: 1 } })
当在 mongodb 中运行该查询时,我收到此错误
"message" : "Unsupported projection option: projection: { item_name: 1.0 }",
但是当我将查询更改为
db.item_m.find({}, { item_name: 1 } )
它工作正常,返回我期望的结果。
我对 express、node.js 和 mongodb 仍然很陌生。这是 mongodb 和 mongoose 之间的一些版本问题吗?我对 mongodb 使用 4.0.13,对 mongoose 使用 5.8.3,但根据我的研究,这应该没问题。我在这里还缺少什么?或者我应该检查哪些我可能错过的东西?提前致谢。
解决方案
我使用这种方法获取所有查询。我已经根据您的需要对其进行了调整。
exports.get_all_item = (req, res, next) => {
Item.find()
.select("item_name")
.exec()
.then(items => {
if (items.length > 0) {
res.status(200).json(items);
} else {
res.status(204).json({
message: "No items available"
});
}
})
.catch(error => {
next(error);
});
};
希望能帮助到你 :)
推荐阅读
- python - 如何在 Visual Studio Code 中导入我自己的 Python 文件?
- java - 无法使用 Log4j 捕获调试模式
- jquery - 使用控制台开发工具在 Chrome 中自动加载 URL
- php - PHP Laravel 代码返回错误 500,日志中没有警告或错误
- javascript - 在 TypeScript\JavaScript 中将数组分解为函数参数
- android - 为什么 Android Studio 无法识别 Android Q Beta 2 符号?
- c# - 属性路由 ASP.NET Core
- java - 是否可以创建多个对象的列表,其中对象带有多个参数?
- python - Python - 一键返回上一页和 .destroy() 页面
- css - 在填充区域内剪辑伪元素