0

I have used to save some data i mongodb database by using this code. When i send to the data to the server using postman successfullt save data in the database. But in server console shows this error message and crash the connection.

router file code

router.post("/timeTableRegistration", function (req, res) {
    const newData = req.body
    // console.log(newTimeTabl)
    const newTimeTable = new classTimeTable(newData)
    console.log(newTimeTable)
    newTimeTable.save()
        .then(result => {
            console.log(result)
            res.json({ state: true, msg: "Data Inserted Successfully..!" });
        })
        .catch(error => {
            console.log(error)
            res.json({ state: false, msg: "Data Inserting Unsuccessfull..!" });
        })
    res.send("Success");
});

model file code

const classTimeTableSchema = mongoose.Schema({

    className: { type: String, require: true },
    monday: [{ 
        one: {type: String, require: true},
        two: {type: String, require: true},
     }],
});
Sachin Muthumala
  • 775
  • 1
  • 9
  • 17
  • 1
    found the error. I have delete the "res.send("Success") and works fine. – Sachin Muthumala Oct 05 '19 at 03:32
  • 1
    Exactly, you'll be calling `res.send` and `res.json` always and you can only call one of these. Voting to close as no longer reproducible. – ggorlen Oct 05 '19 at 03:35
  • 1
    Note that the exact same reasoning is fully described in answers of the existing question with the same error message in the title. When you ask a question you would have been suggested that question and it's answers as a duplicate. Please actually read those suggestions before posting. The other clear problem was not realizing that ANY code present on the "next line after the `save()` **does not wait** for that `save()` to complete. If you wanted something to happen **after** all the other operations, then in belongs in the `.then()`, or a chained `then()`. – Neil Lunn Oct 05 '19 at 04:59

0 Answers0