Sfoglia il codice sorgente

Fix updating recipes bug

Lee Morgan 6 anni fa
parent
commit
d805ecdc27
2 ha cambiato i file con 21 aggiunte e 2 eliminazioni
  1. 16 2
      controllers/home.js
  2. 5 0
      models/recipe.js

+ 16 - 2
controllers/home.js

@@ -110,6 +110,7 @@ module.exports = {
         }
 
         Merchant.findOne({_id: req.session.user})
+            .populate("recipes")
             .then((merchant)=>{
                 axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
                     .then((result)=>{
@@ -133,15 +134,27 @@ module.exports = {
                                 }
                             }
                         }
+                        console.log(result.data.elements);
 
+                        let newRecipes = []
                         for(let recipe of result.data.elements){
-                            merchant.recipes.push({
+                            let newRecipe = new Recipe({
                                 posId: recipe.id,
+                                merchant: merchant._id,
                                 name: recipe.name,
                                 ingredients: []
                             });
+
+                            merchant.recipes.push(newRecipe);
+                            newRecipes.push(newRecipe);
                         }
-                        
+
+                        Recipe.create(newRecipes)
+                            .catch((err)=>{
+                                console.log(err);
+                                return res.render("error");
+                            });
+
                         merchant.save()
                             .then((newMerchant)=>{
                                 newMerchant.populate("recipes.ingredients.ingredient").execPopulate()
@@ -194,6 +207,7 @@ module.exports = {
                 for(let recipe of data.recipes){
                     let newRecipe = {
                         posId: recipe.posId,
+                        merchant: newMerchant._id,
                         name: recipe.name,
                         ingredients: []
                     };

+ 5 - 0
models/recipe.js

@@ -5,6 +5,11 @@ const RecipeSchema = new mongoose.Schema({
         type: String,
         required: true
     },
+    merchant: {
+        type: mongoose.Schema.Types.ObjectId,
+        ref: "Merchant",
+        required: true
+    },
     name: {
         type: String,
         required: true,