0
如何製作回調函數和變量以便將數據保存到數據庫中?如何在節點中進行異步回調並確保HTML表單數據可用於保存到db
我有像下面的節點app.post。我需要將表單數據轉換爲貓鼬模型。我得到的數據是兩個數組,只有大於0的數據被搜索並保存,但這不起作用,我無法繞過它。
我認爲循環和搜索成分應該是一個函數,Meal.save應該在回調中,但我不知道如何。我幾個星期來一直在抨擊這個問題,而且我已經用Google搜索了一下,但這並不對我開放。請幫我解決一下這個。
在此先感謝!
app.js
app.post("/diary/:id/dayshow", function(req, res) {
// Get the day I want to save my meal data
Day.findById(req.params.id, function(err, day) {
if (err) {
console.log(err);
res.redirect("/diary");
} else {
// This is the format I have in the mongoose model
// and I need to convert HTML form data to this
var singleMeal =
{
// title: dinner
// ingredients: [
// {
// ingredient:{},
// amount: Number
// }
// ]
}
var singleMealTempArr = [];
var singleIngredientObj = {};
var amount;
singleMeal.title = req.body.title;
// Looping through the HTML form data and converting it to mongoose model
// I want to loop data and search mongo first and after that I need to save
// the data to mongo which happens in Meal.save()
for (var i = 0; i < req.body.amount.length; i++) {
var _id = req.body.id[i];
amount = Number(req.body.amount[i]);
if (amount !== 0) {
singleIngredientObj.amount = amount;
// Searching ingredient from database with ID what is from the HTML form
Ingredient.findById({_id}, function(err, foundIngredientData){
console.log(foundIngredientData + "<<<<<<<<<<<<< ingredientData");
singleIngredientObj.ingredient = foundIngredientData;
singleMealTempArr.push(singleIngredientObj);
console.log(JSON.stringify(singleMealTempArr) + "<<<<<<<<<<<< JSON.stringify(singleMealTempArr)");
});
}
}
singleMeal.ingredients = singleMealTempArr;
// Now I should have data in singleMeal variable, but it has only title and
// empty array of ingredients
console.log("JSON.stringify(singleMeal) >>>>>>>>>" + JSON.stringify(singleMeal) + "<<<<< JSON.stringify(singleMeal)");
}
Meal.create(singleMeal, function(err, singleMealObject) {
if (err) {
console.log(err);
} else {
console.log("--- You hit the Meal.create + Save -----" + "singleMealObject: " + singleMealObject);
// day.meals.push(singleMealObject); <-- These two are commented because previous steps dont work and singleMealObject has only title and empty array of ingredients
// day.save();
res.redirect("/diary/" + day._id + "/dayshow");
}
});
});
});
console.logs我從上面得到的是在這裏:
JSON.stringify(singleMeal) >>>>>>>>>{"title":"aa","ingredients":[]} <<<<< JSON.stringify(singleMeal)
{ _id: 597c11c04a7bce08cdcdef41,
name: 'oatmeal',
kcal: 100,
protein: 2,
carb: 50,
fat: 1,
addinfo: '',
__v: 0 } <<<<<<<<<<<<< ingredientData
[{"amount":3,"ingredient":{"_id":"597c11c04a7bce08cdcdef41","name":"oatmeal","kcal":100,"protein":2,"carb":50,"fat":1,"addinfo":"","__v":0}}] <<<<<<<<<<<< JSON.stringify(singleMealTempArr)
{ _id: 597c11c04a7bce08cdcdef41,
name: 'oatmeal',
kcal: 100,
protein: 2,
carb: 50,
fat: 1,
addinfo: '',
__v: 0 } <<<<<<<<<<<<< ingredientData
[{"amount":3,"ingredient":{"_id":"597c11c04a7bce08cdcdef41","name":"oatmeal","kcal":100,"protein":2,"carb":50,"fat":1,"addinfo":"","__v":0}},{"amount":3,"ingredient":{"_id":"597c11c04a7bce08cdcdef41","name":"oatmeal","kcal":100,"protein":2,"carb":50,"fat":1,"addinfo":"","__v":0}}]<<<<<<<<<<<< JSON.stringify(singleMealTempArr)
--- You hit the Meal.create + Save -----singleMealObject: { __v: 0,
title: 'aa',
_id: 597c11cb4a7bce08cdcdef61,
ingredients: [] }
餘did't甚至認爲我可以在同一時間搜索多個ID。我會讓你知道,如果我得到這個工作。感謝您的提示。 – Damii
檢查我在答案中添加的鏈接 – Kaps