title: Asynchronous Programming in node js to pass constants/predefined mandatory values through mongoose model
body: <p>I have multiple questions, please go through my code.</p>
  
  <p>1) how to pass constants/predefined mandatory values through model?</p>
  
  <p>For eg. I have some fields which user must be passing the values and some constants to pass on inside the kafkaSchema.config[ ] and also livySchema.args[ ]. The code i want to pass through is in second  question on the same question thread.</p>
  
  <pre><code> const mongoose = require('mongoose');
  
  
  const livy_schema = mongoose.Schema({
      file: { type: String, required: true },
      name: { type: String, required: true },
      className: { type: String, required: true },
      args: [{ type: mongoose.Schema.Types.Mixed, required: true }] //here i have constants to pass on to 
  });
  
  const kafka_schema = mongoose.Schema({
      _id: mongoose.Schema.Types.ObjectId,
      name: { type: String, required: true, unique: false },
      config: { type: mongoose.Schema.Types.Mixed, required: true } //here i have constants to pass on to 
  });
  
  
  const enrichedEventSchema = mongoose.Schema({
      _id: mongoose.Schema.Types.ObjectId,
      projectId: { type: mongoose.Schema.Types.ObjectId, ref: 'Project', required: true },
      name: { type: String, required: true, unique: true },
      description: { type: String, required: false },
      type: { type: String, enum: ["Enriched"], required: true },
      format: { type: String, enum: ["JSON", "DELIMITED", "FixedWidth", "LOG"], required: true },
      kafka: [kafka_schema],
      livy: [livy_schema]  // how to make this schema required:true?
  });
  
  module.exports = mongoose.model('EnrichedEvent', enrichedEventSchema);
  </code></pre>
  
  <p>2) how to make this code to run asynchronously, Right now its working synchronously. For example, Its able to save the eventdata in event collection in database, then its updating the project collection, then calling axios.post method to call my livy server and kafka server in order. What i want to do is save the eventdata in event collection in database, then update the project collection (synchronously), meanwhile I want to call my livy and kafka server at the same time (Asynchronously).</p>
  
  <pre><code>router.post("/:projectId/events/enriched", (req, res, next) =&gt; {
      const enrichedEvent = new EnrichedEvent({
          _id: mongoose.Types.ObjectId(),
          name: req.body.name,
          projectId: req.params.projectId, //taking from url
          description: req.body.description,
          type: req.body.type,
          format: req.body.format,
          kafka: req.body.kafka,
          livy: req.body.livy
      });
      enrichedEvent.save()
          .then(result =&gt; {
              console.log(result);
              res.status(201).json({
                  message: "Event stored",
                  createdEvent: {
                      _id: result._id,
                      projectId: result.projectId,
                      name: result.name,
                      description: result.description,
                      type: result.type,
                      kafka: result.kafka,
                      livy: result.livy
                  }
              });
  
              Project.findOneAndUpdate({ _id: result.projectId },
                  { $push: { enrichedEvents: result._id } })
              axios.post("http://52.xxx.xxx.199:8998/batches", result.livy)
                  .then(function (response) {
                      console.log(response);
                  })
                  .then(axios.get("http://52.xxx.xxx.199:8998/batches/"), function (res) {
                      console.log(res);
                  })
              axios.post("http://52.xxx.xxx.199:8083/connectors", result.kafka)
                  .then(function (response) {
                      console.log(response);
                  })
                  .catch(err =&gt; {
                      console.log(err);
                      res.status(500).json({
                          error: err
                      });
                  });
          });
  });
  </code></pre>
  
  <p>Question may seem bit lengthy, but valid question to ask on SO. Please guide me to right direction.</p>