node.js - 应用程序未从 mongodb 发送或接收数据
问题描述
我正在创建一个简单的练习宁静的应用程序,我正在尝试测试我的路线。
我的路线如下所示:
router.get('/', (req, res) => {
const posts = Post.find();
res.json(posts);
});
router.post('/', (req, res) => {
const post = new Post({
title: req.body.title,
description: req.body.description
});
post.save()
.then(data => {
res.json(data);
})
.catch(err => {
res.json({ message: err});
console.log(err);
})
});
这是我使用 db 连接建立 mongodb 连接以及 .env 文件的地方:
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
require('dotenv/config');
app.use(bodyParser.json());
//Import Routes
const postRoute = require("./routes/posts");
app.use('/posts', postRoute);
app.get('/', (req, res) => {
res.send("home");
});
mongoose.connect(
process.env.DB_CONNECTION,
{ useNewUrlParser: true, //depriciated without this
useUnifiedTopology: true}, //depriciated without this
() => console.log('connected to db'));
app.listen(3000);
//env file:___________________________________________________________
DB_CONNECTION=mongodb+srv://user-0:<test>@cluster0.wb39f.mongodb.net/<test-db>?retryWrites=true&w=majority
当我在邮递员上发送获取请求时,我收到此错误:
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'NativeConnection'
| property 'base' -> object with constructor 'Mongoose'
| property 'connections' -> object with constructor 'Array'
--- index 0 closes the circle
at JSON.stringify (<anonymous>)
at stringify (C:\Users\westo\Documents\code stuff\restful\node_modules\express\lib\response.js:1123:12)
at ServerResponse.json (C:\Users\westo\Documents\code stuff\restful\node_modules\express\lib\response.js:260:14)
at C:\Users\westo\Documents\code stuff\restful\routes\posts.js:7:9
at Layer.handle [as handle_request] (C:\Users\westo\Documents\code stuff\restful\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\westo\Documents\code stuff\restful\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\westo\Documents\code stuff\restful\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\westo\Documents\code stuff\restful\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\westo\Documents\code stuff\restful\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\westo\Documents\code stuff\restful\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\westo\Documents\code stuff\restful\node_modules\express\lib\router\index.js:275:10)
at Function.handle (C:\Users\westo\Documents\code stuff\restful\node_modules\express\lib\router\index.js:174:3)
at router (C:\Users\westo\Documents\code stuff\restful\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (C:\Users\westo\Documents\code stuff\restful\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\westo\Documents\code stuff\restful\node_modules\express\lib\router\index.js:317:13)
at C:\Users\westo\Documents\code stuff\restful\node_modules\express\lib\router\index.js:284:7
当我发送一个发布请求时,我会收到一条超时消息:
POST http://localhost:3000/posts
Error: socket hang up
Request Headers
Content-Type: application/json
User-Agent: PostmanRuntime/7.26.1
Accept: */*
Postman-Token: 59a4d266-3607-4b7b-b952-a843e4e36192
Host: localhost:3000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
这里要求的是 Post Schema:
const mongoose = require('mongoose');
const PostSchema = mongoose.Schema({
title: {
type: String,
required: true
},
description:{
type: String,
required: true
}/*,
date:{
type: Date,
default: Date.now,
required: false
}*/
});
module.exports = mongoose.model('Posts', PostSchema);
我不确定我的代码或数据库有什么问题。根据我看到的所有教程,他们都设置并编写了他们的代码,基本上是我做的。我认为我的应用程序尝试连接到 mlab 的方式可能存在问题,但我不确定。
解决方案
当您使用 find() 时,您必须传递一个对象作为参数来查找所有帖子,并且正文解析器必须是 app.use(bodyParser.urlencoded{ extended: true })
推荐阅读
- python - PYMC3混合模型:帮助理解多变量模型
- dialogflow-es - 来自数据库的 Dialogflow 插槽验证
- image-processing - 理解颜色如何应用于每个像素的问题
- xamarin - 在 Xamarin Forms 中关闭浏览器而无需用户交互
- c# - Asp.Net Core MVC Web 应用程序中的单元测试
- android - Kotlin GoogleMaps ClusterManager 返回的集群项目大小小于最初传递给它的项目大小
- csv - 使用 Overwrite 将 PySpark Dataframe 写入 GCS
- vue.js - 发布新的Vue应用程序版本时如何强制清除chrome中的缓存
- flutter - 如何转换为未知的通用运行时类型(C# ChangeType 等效项)
- python - beautifulsoup for loop 仅提取第一页数据