Estoy tratando de usar la API de recuperación para una solicitud posterior a la base de datos. Estoy creando una plataforma de discusión simple y estoy tratando de permitir que el usuario envíe un formulario que se guarda en mongo.
Esta es mi ruta en mi archivo handlers.js :
router.post('/add', async (req, res) => { console.log('inside post method') const userComment = { userid: "testUser" + stringify(Math.random()), commentid: "comment" + stringify(Math.random()), content: req.body, reply: false } //const userComment = req.body const newComment = new commentModel(userComment) try { await newComment.save(async (err, newCommentResult) => { console.log('New comment created!') res.end('New commentcreated!') }) } catch (err) { console.log(err) res.end('Comment not added!') } })Esta es mi búsqueda en mi front-end:
function addComment (content) { console.log(content) //**** NOT DONE , NEW COMMENT ADD INTO DATABASE */ //!!!!! Call api request here !!!!// const type = { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(content), mode: 'cors' } fetch('/add', type) .then( console.log('success') ) .catch( error => console.log('error', error) ) }Básicamente, estoy tratando de enviar datos a través de un formulario que se guarda en mongo.