gpt4 book ai didi

node.js - 使用node.js从mongoDB获取数据,连接有效但没有数据显示

转载 作者:太空宇宙 更新时间:2023-11-03 23:51:24 26 4
gpt4 key购买 nike

所以我使用node.js创建了一个应用程序并将其连接到mongoDB,但是没有显示数据,它只是显示空的json数据文件[]。数据库名称为 sample_geospatial,集合名为 shipwrecks,所有 IP 地址都可以连接。

为什么我在浏览器中显示数据的任务没有成功?

This is my app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var shipwrecksRouter = require('./routes/shipwrecks');

var app = express();

// Database connect with moongoose
var mongoose = require('mongoose');
mongoose.connect('mongodb+srv://gockzor:***********@cluster0-boypg.azure.mongodb.net/test?retryWrites=true&w=majority');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {
console.log("Kopplingen lyckades!");
});

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({
extended: false
}));

app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/shipwrecks', shipwrecksRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
module.exports = app;

This is my model

var mongoose = require('mongoose');

var WrecksSched = new mongoose.Schema({
recrd: String,
vesslterms: String,
feature_type: String,
chart: String,
latdec: Number,
londec: Number,
gp_quality: String,
depth: String,
sounding_type: String,
history: String,
quasou: String,
watlev: String,
coordinates: Array,
}, {
collection: 'shipwrecks'
});
module.exports = mongoose.model('ShipwrecksModel', WrecksSched);

This is my routes file

var express = require('express');
var router = express.Router();

var mongoose = require('mongoose');
var ShipwrecksModel = require('../models/ShipwrecksModel.js');

router.get('/', function (req, res, next) {
ShipwrecksModel.find(function (err, wrecks) {
if (err) return next(err);
else {
res.json(wrecks);
}
});
});
module.exports = router;

最佳答案

将其作为答案:

如果您在 api 响应中看到 [] 而不是错误 -

then the issue could either be you're not able to connect to proper collection name(if you're sure collection has data in it) or correct db where your collection exists.

因此,从您的数据库网址来看:

'mongodb+srv://gockzor:***********@cluster0-boypg.azure.mongodb.net/test?retryWrites=true&w=majority'

您已连接到test数据库,那么您的收藏shipwrecks就是在您的数据库中吗?如果不是,请尝试在连接后切换到实际数据库,或将 url 中的 test 替换为实际数据库名称。

所以默认情况下你会得到一个 url w.r.t.当您从 Atlas mongoDB 获取数据库时,将其发送到 test 数据库。因此,请一次性将其替换为 url,或者您可以在连接到数据库后使用 useDb 函数切换代码。 例如。 :-

从代码切换数据库的基本方法::

 async function myDbConnection() {

const url = mongodb+srv://gockzor:***********@cluster0-boypg.azure.mongodb.net/test?retryWrites=true&w=majority';

try {
await mongoose.connect(url, { useNewUrlParser: true });
console.log('Connected Successfully')
mongoose.connection.useDb('myDB'); // Switching happens here..
/**
* Do some DB transaction with mongoose models as by now models has already been registered to created DB connection
*/
} catch (error) {
console.log('Error connecting to DB ::', error);
}
}

或者你可以直接在 url 中添加类似的内容::

const dbUrl = mongodb+srv://gockzor:***********@cluster0-boypg.azure.mongodb.net/myDB?retryWrites=true&w=majority'

关于node.js - 使用node.js从mongoDB获取数据,连接有效但没有数据显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59327800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com