0

Mongo save callback is not triggering, but the document is getting saved in the db, Heres my code:

MongoDb Version : 6.0.1

Mongoose Version : 6.8.2

The call back not returning anything, but the documents are getting saved without any issues, why the call back is not triggering?

const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const url = 'mongodb://127.0.0.1:27017/musicapp';
const cors = require('cors');
var jquery = require('jquery');

//Mongodb
mongoose.connect(url, { useNewUrlParser: true });
mongoose.set('strictQuery', true);
var conn = mongoose.connection;

conn.on('connected', function () {
  console.log("Database Connected");
})

var artistSchema = new mongoose.Schema({
  artistName: String,
  artistDob: String,
  artistBio: String
})

const artists = mongoose.model("artist", artistSchema);

function addArtist(aName, aDOB, aBIO) {
  const newArtist = new artists({
    artistName: aName,
    artistDob: aDOB,
    artistBio: aBIO
  })
  newArtist.save((err, data, numAffected) => {
    if (!err) {
      return true;
    }
    else {
      return false;
    }
  });

}

//MongoDb
app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html")
})

app.post("/addsongs", cors(), (req, res) => {
  console.log("Add songs");
})

app.post("/addartist", cors(), (req, res) => {
  var artistName = req.body.modalartistname;
  var artistDOB = req.body.modalartistdob;
  var artistBio = req.body.modalartistbio;
  var addedArtist = addArtist(artistName, artistDOB, artistBio);
  if (addedArtist) {
    res.sendStatus(200);
  }
  else {
    console.log("Failure")
    res.sendStatus(400);
  }
  //console.log("Artist Added Sucessfully");
})

app.listen(3000, function () {
  console.log("The Server is up and running");
})
  • How did you determine that the save callback is not triggering? – Aurast Dec 31 '22 at 14:15
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Aurast Dec 31 '22 at 14:17

0 Answers0