【发布时间】:2020-02-21 17:48:32
【问题描述】:
GET 有效。 DELETE 有效。
无法弄清楚为什么 POST 不能使用像这样简单的东西。
{"akey":"avalue"}
使用 Postman 进行测试。 Postman 的错误是“Could not get any response”,这很奇怪,因为我对 GET 和 DELETE 没有任何问题。
Mongo/Node 的新手。按照 Brad Traversy 的 https://www.youtube.com/watch?v=j55fHUJqtyw 教程,了解 Vue、Mongo、Express、Node。
有什么突出的吗?
const express = require( 'express' );
const mongodb = require( 'mongodb' );
const router = express.Router();
// GET POSTS
router.get( '/', async ( req, res ) => {
const posts = await loadPostsCollection();
res.send( await posts.find( {} ).toArray() );
} );
// ADD POST
router.post( '/', async ( req, res ) => {
const posts = await loadPostsCollection();
await posts.insertOne( {
text: req.body.text
} );
res.status(201).send();
} );
// DEL POST
router.delete('/:id', async (req, res)=>{
const posts = await loadPostsCollection();
await posts.deleteOne({_id: new mongodb.ObjectID(req.params.id)});
res.status(200).send();
})
async function loadPostsCollection() {
const client = await mongodb.MongoClient.connect( 'mongodb+srv://someUser:somePassword@some-bkebp.mongodb.net/test?retryWrites=true&w=majority', {
useNewUrlParser : true,
useUnifiedTopology: true
} );
return client.db( 'someDB' ).collection( 'somCollection' )
}
module.exports = router;
【问题讨论】: