在 NodeJS API 调用中推送和拉取数组中的元素
Push and Pull elements in Array in NodeJS API call
在我的应用程序中,我使用 Mongoose 创建模式。我有一个用户架构和一个 Tickit 架构。每次用户创建一个 tickit 时,我想将 tickit 的 id 添加到我的用户模式的 'tickits' 数组中,如下所示。代码工作了一半——当我创建一个 tickit 时,该 id 进入用户 tickits 数组——但是当我删除 tickit 时,该 id 并没有从数组中删除。
Tickit.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var tickitSchema = new Schema({
title: String,
description: String,
author : { type: Schema.Types.ObjectId, ref: 'User' },
comments: [{body:"string", by: Schema.Types.ObjectId}]
});
module.exports = mongoose.model('Tickit', tickitSchema);
User.js
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var Schema = mongoose.Schema;
var Tickit = require('../models/Tickit');
var userSchema = new Schema({
email : String,
password : String,
tickits : [Tickit.tickitSchema]
});
userSchema.methods.generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.password);
};
module.exports = mongoose.model('User', userSchema);
我创建和删除tickits的函数如下:
exports.create = function(req, res) {
var tickit = new Tickit(req.body);
var user = req.user;
tickit.author = user._id;
user.update({tickits : {id : tickit._id}}, {$push: {tickits: { id: tickit._id}}},function (err) {
if (err)
return res.send(err);
});
user.save();
tickit.save(function(err, tickits) {
if (err) {
res.send(err);
} else {
res.json(tickits);
}
});
};
exports.destroy = function(req, res) {
var tickit_id = req.params.tickit_id;
req.user.update({tickits : {id : tickit_id}}, {$pull: {tickits: { id: tickit_id}}},function (err) {
if (err)
return res.send(err);
});
req.user.save();
Tickit.remove({_id : req.params.tickit_id}, function(err, tickit) {
if (err)
res.send(err);
Tickit.find(function(err, tickits) {
if (err)
res.send(err)
res.json(tickits);
});
});
}
也许我以错误的方式解决这个问题,但我已经尝试了几个小时,任何帮助都会非常有用。
这看起来像是 save
的异步性质的问题。此外,稍微清理一下架构以帮助澄清。
var Schema = mongoose.Schema;
var tickitSchema = Schema({
title: String,
description: String,
author : {
type: Schema.Types.ObjectId,
ref: 'User'
},
comments: [{
body: String,
by: {
type: Schema.Types.ObjectId,
ref: 'User'
}
}]
});
var userSchema = Schema({
email: String,
password: String,
tickits: [{
type: Schema.Types.ObjectId,
ref: 'Tickit'
}]
});
module.export.create = function createTickit(req, res) {
var tickit = Tickit(req.body);
tickit.author = req.user;
// Save the ticket first to ensure it exists
tickit.save(function (err, tickit) {
if (err || tickit === null) { return res.send(err || new Error('Ticket was not saved but there was no error...odd')); }
User.findByIdAndUpdate(req.user.id, {$push: {tickits: tickit}}, {new: true}, function (err, user) {
if (err) { return res.send(err) };
res.json(ticket); // or whatever else you want to send
});
});
};
module.export.destroy = function destroyTickit(req, res) {
// Remove the ticket first to ensure it is removed
Tickit.findByIdAndRemove(req.params.tickit_id, function (err, tickit) {
if (err || tickit === null) { return res.send(err || new Error('Ticket was not removed but there was no error...odd')); }
User.findByIdAndUpdate(req.user.id, {$pull: {tickits: tickit.id}}, {new: true}, function (err, user) {
if (err) { return res.send(err) };
res.json(ticket); // or whatever else you want to send
});
});
};
这将执行基本的 create/destroy 但没有任何安全控制(确保谁有权销毁门票等)。
在我的应用程序中,我使用 Mongoose 创建模式。我有一个用户架构和一个 Tickit 架构。每次用户创建一个 tickit 时,我想将 tickit 的 id 添加到我的用户模式的 'tickits' 数组中,如下所示。代码工作了一半——当我创建一个 tickit 时,该 id 进入用户 tickits 数组——但是当我删除 tickit 时,该 id 并没有从数组中删除。
Tickit.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var tickitSchema = new Schema({
title: String,
description: String,
author : { type: Schema.Types.ObjectId, ref: 'User' },
comments: [{body:"string", by: Schema.Types.ObjectId}]
});
module.exports = mongoose.model('Tickit', tickitSchema);
User.js
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var Schema = mongoose.Schema;
var Tickit = require('../models/Tickit');
var userSchema = new Schema({
email : String,
password : String,
tickits : [Tickit.tickitSchema]
});
userSchema.methods.generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.password);
};
module.exports = mongoose.model('User', userSchema);
我创建和删除tickits的函数如下:
exports.create = function(req, res) {
var tickit = new Tickit(req.body);
var user = req.user;
tickit.author = user._id;
user.update({tickits : {id : tickit._id}}, {$push: {tickits: { id: tickit._id}}},function (err) {
if (err)
return res.send(err);
});
user.save();
tickit.save(function(err, tickits) {
if (err) {
res.send(err);
} else {
res.json(tickits);
}
});
};
exports.destroy = function(req, res) {
var tickit_id = req.params.tickit_id;
req.user.update({tickits : {id : tickit_id}}, {$pull: {tickits: { id: tickit_id}}},function (err) {
if (err)
return res.send(err);
});
req.user.save();
Tickit.remove({_id : req.params.tickit_id}, function(err, tickit) {
if (err)
res.send(err);
Tickit.find(function(err, tickits) {
if (err)
res.send(err)
res.json(tickits);
});
});
}
也许我以错误的方式解决这个问题,但我已经尝试了几个小时,任何帮助都会非常有用。
这看起来像是 save
的异步性质的问题。此外,稍微清理一下架构以帮助澄清。
var Schema = mongoose.Schema;
var tickitSchema = Schema({
title: String,
description: String,
author : {
type: Schema.Types.ObjectId,
ref: 'User'
},
comments: [{
body: String,
by: {
type: Schema.Types.ObjectId,
ref: 'User'
}
}]
});
var userSchema = Schema({
email: String,
password: String,
tickits: [{
type: Schema.Types.ObjectId,
ref: 'Tickit'
}]
});
module.export.create = function createTickit(req, res) {
var tickit = Tickit(req.body);
tickit.author = req.user;
// Save the ticket first to ensure it exists
tickit.save(function (err, tickit) {
if (err || tickit === null) { return res.send(err || new Error('Ticket was not saved but there was no error...odd')); }
User.findByIdAndUpdate(req.user.id, {$push: {tickits: tickit}}, {new: true}, function (err, user) {
if (err) { return res.send(err) };
res.json(ticket); // or whatever else you want to send
});
});
};
module.export.destroy = function destroyTickit(req, res) {
// Remove the ticket first to ensure it is removed
Tickit.findByIdAndRemove(req.params.tickit_id, function (err, tickit) {
if (err || tickit === null) { return res.send(err || new Error('Ticket was not removed but there was no error...odd')); }
User.findByIdAndUpdate(req.user.id, {$pull: {tickits: tickit.id}}, {new: true}, function (err, user) {
if (err) { return res.send(err) };
res.json(ticket); // or whatever else you want to send
});
});
};
这将执行基本的 create/destroy 但没有任何安全控制(确保谁有权销毁门票等)。