2015-09-28 79 views
0

所以我有两个猫鼬的模型:保存对象到另一个模式阵列

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
var eventSchema = new mongoose.Schema({ 

    name: String, 
    date: String, 
    dogs: [{ type: Schema.Types.ObjectId, ref: 'Dog' }] 

}); 
module.exports = mongoose.model('Event', eventSchema); 

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
var dogSchema = new mongoose.Schema({ 

    name: String, 
    age: String, 
    gender: String, 

}); 
module.exports = mongoose.model('Dog', dogSchema); 

Event包含dogs数组和IM试图找出如何添加/删除狗给这个阵列。

在客户端上我得到了这个方法:

$.ajax({ 
    url: "http://localhost:3000/api/events/", 
    dataType: 'json', 
    type: 'POST', // Not sure if I should Post or Put... 
    data: {event_Id : this.props.choosenEvent._id, //Here I got the Id of the Event that i want to update by 
      dog_Id : this.props.events[dog]._id }, //adding this dog, which Id is here 
    success: function(data) { 

    }.bind(this), 
    }); 
}, 

在服务器上,的NodeJS,我得到了我的路线的API。对我来说,使用PUT方法是有道理的,并且首先通过将event_Id作为参数传递来获取正确的事件。像这样的:

router.route('/events/:event_id') 
    .put(function(req, res) { 


     Event 
     .findById({ _id: req.param.event_id }) 
     .populate('dogs') 

     }); 

但我在这一点卡住了。任何帮助赞赏。谢谢!

更新!

谢谢!你的代码有很多帮助,你使用lodash。删除数组中的狗,是否有类似的方式来添加一个项目与lodash?

我给Add方法这样一去:

router.route('/events') 
     .post(function(req, res) { 
      // Your data is inside req.body 

      Event 
       .findById({ _id: req.body.event_Id }) 
       // execute the query 
       .exec(function(err, eventData) { 
        // Do some error handing 
        // Your dogs are inside eventData.dogs 
        eventData.dogs.push(req.body.dog_Id); 
        console.log(eventData) 

       }); 
        // Update your eventDate here 
        Event.update({_id: req.body.event_id}, eventData) 
         .exec(function(err, update) { 
          // Do some error handing 
          // And send your response 
         }); 
       }); 

当我打的console.log(eventData)我可以看到dog_id被添加到阵列,因为它应该。但它不会保存到数据库,并且错误说eventData未在Event.Update中定义。我怀疑这是一个Js范围问题。

是博格尔斯我

Onte事情是这样的:

很显然,我希望能够添加和删除阵列和 路线狗是这样的:router.route('/events')

但是,如果add-method和remove-method都在同一条路径上,那么代码如何知道我要去哪?

回答

1

您正在犯的一些错误。首先,您要发送POST请求,但您的路由接受PUT请求。我已更新您的代码,以便它接受POST。

发布对象时,您的数据位于req.body之内。 req.params用于url参数。使用PUT请求时也是如此。

填充是不是真的有必要。您正在将您的dog_id发送到您的功能,以便您可以从阵列中删除您的项目,从而将您的狗从您的事件中移除。这应该做的伎俩。请注意,这不会将您的狗从您的数据库中移除,而只会从您的活动中移除。

最后但并非最不重要。我正在使用lodash_.remove是lodash功能。你一定要检查一下,它会帮助你很多。

看看我的代码。它应该让你去:

router.route('/events/:event_id') 
    // Since you are posting, you should use POST from JavaScript instead of PUT 
    .post(function(req, res) { 
     // Your data is inside req.body 
     Event 
      .findById({ _id: req.body.event_id }) 
      // execute the query 
      .exec(function(err, eventData) { 
       // Do some error handing 
       // Your dogs are inside eventData.dogs 
       _.remove(eventData.dogs, function(d) { 
        return d._id === req.body.dog_Id; 
       }); 
       // Update your eventDate here 
       Event.update({_id: req.body.event_id}, eventData) 
        .exec(function(err, update) { 
         // Do some error handing 
         // And send your response 
        }); 
      }); 

     }); 

UPDATE

我不认为有一种方法将项目添加到与lodash一个数组,但你可以简单地使用就像你在做你的代码示例。这工作得很好。

您的更新无效,因为您正在执行findById并同时进行更新。你必须先找到这个项目,然后添加这个id然后更新这个项目:)在你的findById函数的回调中移动你的更新函数,这个函数应该被修复。所以它看起来像这样:

router.route('/events') 
    .post(function(req, res) { 
     // Your data is inside req.body 

     Event 
     .findById({ _id: req.body.event_Id }) 
     // execute the query 
     .exec(function(err, eventData) { 
      // Do some error handing 
      // Your dogs are inside eventData.dogs 
      eventData.dogs.push(req.body.dog_Id); 
      console.log(eventData) 

      // Update your eventDate here 
      Event.update({_id: req.body.event_id}, eventData) 
      .exec(function(err, update) { 
       // Do some error handing 
       // And send your response 
      }); 
     }); 
    }); 

只要方法与其他方法不同,您可以在相同的路由上添加不同的功能。看看这个answer处的REST。你可以有一个GETPOSTPUT & /事件 DELETE。这是由此规则定义的:

router.route('/events').post(); 
+0

非常感谢!你把我放在正确的轨道上。如果可以,请查看更新。 – user2915962

+0

@ user2915962我已经更新了我的答案。 –

+1

非常感谢!这篇文章对我来说会派上用场。 – user2915962

相关问题