无法在findOne之后更新架构内的架构

时间:2017-07-31 18:35:28

标签: node.js post mongoose

嘿伙计我试图在我的POST请求中更新我的餐厅架构内的FoodSchema这是我的架构:

//Create food schema 
const FoodSchema = new Schema ({
   nameOfFood: String,
   numOfVotes: Number,
});

//Create restaurant schema
const RestaurantSchema = new Schema ({
    nameOfRest: String,
    favoriteFoods:[FoodSchema],
});

我试图在找到餐馆后更新else语句中的食物架构,虽然我能够在.then方法中访问restdata中的数据,但它并没有推动新餐厅。我试图完成的目标是,如果找到餐厅,我会更新其食品架构。

router.post('/votes', function(req,res)
{

    //Parse the data.
    var newString = toTitleCase(req.body.restaurant);

    Restaurant.findOne({nameOfRest:newString}).then(function(restdata)
    {

        //If can't find restaurant, redirect to vote page again.
        if(!restdata)
        {
            console.log("Undefined");
            res.redirect('/votes');
        }
        //Restaurant is found 
        //If the food can't be found create and push a new array with one vote.
        //If you can find the food, then just update and add a vote.
        else
        {

            if(restdata.favoriteFoods.length==0)
            {
                restdata.favoriteFoods.push({nameOfFood:req.body.food,numOfVotes:1});
            }
            else
            {
                restdata.favoriteFoods.numOfVotes++;
            }
        }
    });
});

我正在测试这段代码:

restdata.favoriteFoods.push({nameOfFood:req.body.food,numOfVotes:1});

然而,当我试图推动它时,它不起作用。我在考虑使用findOneAndUpdate,但我不确定这是否是最好的选择。有任何想法吗?

1 个答案:

答案 0 :(得分:0)

好吧我解决了它:这不是最有效的方式,但是,在这里它是。我使用了Talha的保存方法。

 //votes POST route
router.post('/votes', function(req,res)
{

    //Parse the data.
    var newString = toTitleCase(req.body.restaurant);

    Restaurant.findOne({nameOfRest:newString}).then(function(restdata)
    {

        //If can't find restaurant, redirect to vote page again.
        if(!restdata)
        {
            console.log("Undefined");
            res.redirect('/votes');
        }
        //Restaurant is found 
        //If the food can't be found create and push a new array with one vote.
        //If you can find the food, then just update and add a vote.
        else
        {
            //Parse the food string data.
            var newFoodString = toTitleCase(req.body.food); 

            //If there's no food data, initialize it. 
              if(restdata.favoriteFoods.length===0)
              {
                restdata.favoriteFoods.push({nameOfFood:newFoodString,numOfVotes:1});

                restdata.save().then(function(data){
                    console.log(data);
                });
              }
              else
              {
                var flag = false;
                for(var count = 0; count < restdata.favoriteFoods.length; count++)
                {
                    //If you can find the food. Just add a vote.
                    if(restdata.favoriteFoods[count].nameOfFood==newFoodString)
                    {
                        flag = true;

                        restdata.favoriteFoods[count].numOfVotes++;

                        restdata.save().then(function(data){
                            console.log(data);
                        });

                    }

                }
                //Otherwise push it as a new food.
                if(!flag)
                {
                    restdata.favoriteFoods.push({nameOfFood:newFoodString,numOfVotes:1});

                    restdata.save().then(function(data){
                        console.log(data);
                    });
                }
              }

        }
    });
});

    return router;
}