gpt4 book ai didi

node.js - MongoDB 不使用 NodeJS 驱动程序应用程序返回数据

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

我正在使用 mongoose 连接 mongoDB。它是一个nodeJS应用程序。现在我关心的是获取数据并将其显示在我的应用程序中。但是,它不显示任何错误,也不显示任何数据。这是我的服务器文件

const express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
cors = require('cors'),
mongoose = require('mongoose'),
config = require('./DB');

const employeeRoute = require('./routes/employee.route');
mongoose.Promise = global.Promise;

mongoose.connect(config.DB, { useNewUrlParser: true })
.then(() => { console.log('Database is connected') },
err => { console.log('Can not connect to the database' + err) }
);

const app = express();
app.use(bodyParser.json());
app.use(cors());
app.use('/Employees', employeeRoute);
let port = process.env.PORT || 4000;

const server = app.listen(port, function () {
console.log('Listening on port ' + port);
})

这是我的路线文件

const express = require('express');
const employeeRoute = express.Router();

let Employees = require('../models/Employee');

employeeRoute.route('/').get(function (req, res) {
let employees = [];
Employees.find({}, function (err, results) {
if (err) {
console.log(err);
}
else {
employees = results;
console.log(employees)
res.json(employees);
}
})
})
module.exports = employeeRoute

最后是我的 employee.js 文件

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

let Employee = new Schema({
first_name: { type: String },
last_name: { type: String },
title: { type: String },
email: { type: String },
gender: { type: String },
location: { type: String },
phone: { type: Number }
},
{ collation: 'Employees' });
module.exports = mongoose.model('Employees', Employee)


应用程序运行良好,没有任何错误。在将其发布到论坛之前,我仔细检查了 dbcollection 名称。任何建议

最佳答案

看起来您可能只需要稍微重构您的路线,以确保获得您想要的结果。例如,看看我的以下示例:

const express = require('express');
const employeeRoute = express.Router();

let Employees = require('../models/Employee');

employeeRoute.route('/').get(function (req, res) {
// Extract data from the request object here in order to search for employees. For example:
const name = req.body.name;
const surname = req.body.surname;

let employees = [];

// Now perform a find on your specific data:
Employees.find({ first_name: name, last_name: surname }, function(err, results) {
if (err) console.log(err);
else employees = results;

console.log(JSON.stringify(employees));
res.json(employees);
});

// Or you can perform a find on all employees
Employees.find({}, function(err, results) {
if (err) console.log(err);
else employees = results;

console.log(JSON.stringify(employees));
res.json(employees);
});

})
module.exports = employeeRoute

正如我在评论中提到的,您的连接可能需要更改以使用正确的术语:useNewUrlParser

编辑

我还注意到,在您的员工架构中,您放置了 collat​​ion 而不是 collection - 见下文:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

let Employee = new Schema({
first_name: { type: String },
last_name: { type: String },
title: { type: String },
email: { type: String },
gender: { type: String },
location: { type: String },
phone: { type: Number }
}, { collection: 'employees' });

module.exports = mongoose.model('Employees', Employee)

尝试一下,有什么不同吗?

关于node.js - MongoDB 不使用 NodeJS 驱动程序应用程序返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57623317/

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