0

I am trying to make a single Post request to this endpoint:

.route('/users/:userId/recipes')

The request should create the recipe and an array of flavors. So a recipe should contain 1 to many flavors. The api is going to be used by a website and an Android application. Is it possible to achieve this with a single request? if so, how can I achieve this?

I want the result to look something like this.

 {
_id: "j3232343424",
name: "Strawbery bla bla",
amount: 30,
pg: 70,
vg: 30,
flavors: [
            {
              name: "Strawberry",
              percentage: 90
            },
          {
              name: "Cream",
              percentage: 10
            }
          ]
 } 

I have two schemas, one for the recipe, and the other for the flavorings. The recipe Schema has an array of flavors.

var flavorSchema = mongoose.Schema({
name: {
    type: String,
        required: true
},
percentage: {
    type: Number,
        required: true
}
});

var recipeSchema = mongoose.Schema({
name: {
    type: String,
    required: true
},
amount: {
    type: Number,
    required: true
},
pg: {
    type: Number,
    min: 0,
    max: 100,
    required: true
},
vg: {
    type: Number,
    min: 0,
    max: 100,
    required: true
},
flavors: [flavorSchema],
aye
  • 23
  • 4
  • Duplicate of : [Mongodb $push in nested array](https://stackoverflow.com/questions/27874469/mongodb-push-in-nested-array) and you really should learn how to use [`$push`](https://docs.mongodb.com/manual/reference/operator/update/push/) and the other atomic update operators as well. Using `.save()` is okay for "noobs" just grasping concepts, but real solid code simply should not be using it. – Neil Lunn Nov 16 '17 at 00:20
  • But is it possible to make it all happen in a single request? I`ve updated my question, so you can get a better understanding of what i am trying to achieve. – aye Nov 16 '17 at 16:37
  • Read the link you were actually given. That IS a single request using `$push`. That's the point. – Neil Lunn Nov 16 '17 at 20:38

0 Answers0