gpt4 book ai didi

javascript - 将对象保存到另一个模型中的数组

转载 作者:行者123 更新时间:2023-12-03 08:47:24 25 4
gpt4 key购买 nike

所以我有两个 Mongoose 模型:

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 数组,我正在尝试找出如何向该数组添加/删除狗。

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

  $.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 .remove 从数组中删除了一只狗,是否有类似的方法来使用 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 范围问题。

令我困惑的是:

显然我希望能够在数组中添加和删除狗以及路线是这样的:router.route('/events') .

但是如果添加方法和删除方法都在同一条路线上,代码如何知道我要使用哪一个?

最佳答案

您犯了一些错误。首先,您正在发出 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
});
});

});

更新:

我认为没有办法使用 lodash 将项目添加到数组中,但您可以像在代码示例中那样简单地使用 push 。效果很好。

您的更新不起作用,因为您正在同时执行 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
});
});
});

您可以在同一条路线上添加不同的功能,只要方法与其他路线不同即可。看看 REST answer 。您可以在/events上进行GETPOSTPUTDELETE。这是由以下规则定义的:

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

关于javascript - 将对象保存到另一个模型中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32824206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com