【发布时间】:2021-04-16 21:24:19
【问题描述】:
我正在尝试通过 node、express、mongoose 和 heroku 简单地发布到我的 MongoDB Atlas db。邮递员 POST 请求,带有正文的原始 JSON:
{
"title": "heroku post",
"description": "post me plsssss"
}
使用这个确切的代码在 localhost 上工作,但是当通过 heroku 上传时,try/catch 块在 post.save() 处失败,因为响应是错误。
{
"error": "there's an error",
"message": {}
}
但是错误是空的,我不知道如何调试它。我已经在app.js 中输入了mongoose.set('debug', true);,并且我修改了我的package.json:"start": "node app.js DEBUG=mquery",,但是这些并没有产生我所看到的额外输出。有没有其他方法可以知道为什么 post.save() 会引发错误,一些我没有使用的日志..以及如何查看这些日志?或者,如果您只是知道问题所在?
App.js
//use dotenv to store secret keys
require("dotenv").config();
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const cors = require('cors');
//connect to DB
mongoose.connect("mongodb+srv://grushevskiy:intercom@cluster-rest.4luv0.mongodb.net/cluster-rest?retryWrites=true&w=majority", { useNewUrlParser: true, useUnifiedTopology: true, dbName: "cluster-rest" }, () => {
console.log('connected to DB!')
})
mongoose.set('debug', true);
const db = mongoose.connection;
db.once('open', () => {
console.log('connection opened')
});
//import routes for middleware
const postsRoute = require('./routes/posts');
//midleware routes
app.use('/posts', postsRoute)
//ROUTES
app.get('/', (req,res) => {
res.send('we are on home')
})
//MIDDLEWARES
//cors
app.use(cors());
//decode url special characters
app.use(express.urlencoded({ extended: true }));
//parse json POSTs
app.use(express.json());
//How do we start listening to the server
app.listen( process.env.PORT || 3000);
posts.js
const express = require ('express')
const router = express.Router();
const Post = require('../models/Post')
//SUBMIT A POST
router.post('/', async (req,res) => {
const post = new Post({
title: req.body.title,
description: req.body.description
});
console.log(post)
try {
const savedPost = await post.save();
res.json(savedPost);
console.log(savedPost)
} catch (err) {
res.json({ error: "there's an error", message: err, })
}
})
module.exports = router;
Post.js 模型
const mongoose = require('mongoose')
const PostSchema = mongoose.Schema({
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
})
module.exports = mongoose.model('Post', PostSchema)
当我输入heroku logs --tail 时没有错误,最初也是“连接到数据库!”消息来晚了..我想知道这是否是异步/等待的问题?我的 package.json:
{
"name": "22-npmexpressrestapi",
"version": "1.0.0",
"engines": {
"node": "14.15.3",
"npm": "6.14.9"
},
"description": "",
"main": "index.js",
"scripts": {
"start": "node app.js DEBUG=mquery",
"start:dev": "node app.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.7"
},
"dependencies": {
"dotenv": "^8.2.0",
"express": "4.17.1",
"cors": "^2.8.5",
"mongoose": "^5.11.11"
}
}
【问题讨论】:
-
连接可能已断开。尝试在你的 mongoose 连接函数中添加一个 catch 错误,或者将它包装在一个 try/catch 中,看看它是否成功连接。
-
你可以试试
res.json({ error: "there's an error", message: JSON.stringify(err) })吗? -
Alejandro 不知道该怎么做.. thammada.ts.. 没有改变输出
标签: javascript node.js mongodb heroku mongoose