While saving data to MongoDB, the characters that generated from a text area isn’t generates correctly within the database, the output is just none (”), I’ve tried to change the input’s unicode but nothing appeared to work. I will appreciate your help to let me know what I did wrong – and of course I will learn from it 😉
My whole JS’ code
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
var http = require('http').Server(app)
var io = require('socket.io')(http)
var mongoose = require('mongoose')
app.use(express.static(__dirname))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
mongoose.Promise = Promise
var dbUrl = 'mongodb+srv://username:[email protected]/database'
var Message = mongoose.model('Message', {
name: String,
message: String
})
app.get('/messages', (req, res) => {
Message.find({}, (err, messages) => {
res.send(messages)
})
})
app.get('/messages/:user', (req, res) => {
var user = req.params.user
Message.find({name: user}, (err, messages) => {
res.send(messages)
})
})
app.post('/messages', async (req, res) => {
try {
var message = new Message(req.body)
var savedMessage = await message.save()
console.log('saved')
var censored = await Message.findOne({ message: 'badword' })
if (censored)
await Message.remove({ _id: censored.id })
else
io.emit('message', req.body)
res.sendStatus(200)
} catch (error) {
res.sendStatus(500)
return console.error(error)
} finally {
console.log('message post called')
}
})
mongoose.connect(dbUrl, { useMongoClient: true }, (err) => {
console.log('MongoDB status is', err)
})