Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
app.post connection and middleware in nodejs

app.post is not working on postman and showing result in not in JSON format and also middleware is showing error in postman what m doing wrong in this?? is there any new middleware code introduced recently??

// importing
import express from "express";
import mongoose from "mongoose";
import Messages from "./dbMessages.js";

//app config
const app = express();
const port = process.env.PORT || 9000;

//middleware
app.use(express.json());

//DB config
const connection_url =
  "mongodb+srv://admin:0ZKon3XRmT0SWkal@cluster0.7pljv.mongodb.net/whatsappdb?retryWrites=true&w=majority";

mongoose.connect(connection_url, {
  // useCreateIndex: true,
  // useNewUrlParser: true,
  // useUnifiedTopology: true,
});

//????

//api routes
app.get("/", (req, res) => res.status(200).send("hello world"));


app.post("/messages/new", (req, res) => {
  const dbMessage = req.body;

  Messages.create(dbMessage, (err, data) => {
    if (err) {
      res.status(500).send(err);
    } else {
      res.status(201).send(data);
    }
  });
});

//listen
app.listen(port, () => console.log(`Listening on localhost:${port}`));

dbmessages.js is given below

import mongoose from "mongoose";

const whatsappSchema = mongoose.Schema({
  message: String,
  name: String,
  timestamp: String,
  received: Boolean,
});

export default mongoose.model("messageContent", whatsappSchema);
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You are not creating the instance of a model.

app.post("/messages/new", (req, res) => {
  const dbMessage = req.body,
        messageModel = new Messages();

  message.Modelmessage = req.body.Modelmessage;
  message.Modelname = req.body.Modelname;
  messageModel.timestamp = req.body.timestamp;
  messageModel.received = req.body.received;
 
  messageModel.save().then(data => {
     res.status(201).send(data);
  }).catch((e) => {
     res.status(500).send(err);
  })

     

});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!