I've noticed there's little documentation and info about how I should manipulate an array of objects using Mongoosejs.
I have the following model/Schema for an User:
'use strict';
/**
* Module Dependencies
*/
var bcrypt = require('bcrypt-nodejs');
var crypto = require('crypto');
var mongoose = require('mongoose');
/**
* Custom types
*/
var ObjectId = mongoose.Schema.Types.ObjectId;
var userSchema = new mongoose.Schema({
email: { type: String, unique: true, index: true },
password: { type: String },
type: { type: String, default: 'user' },
facebook: { type: String, unique: true, sparse: true },
twitter: { type: String, unique: true, sparse: true },
google: { type: String, unique: true, sparse: true },
github: { type: String, unique: true, sparse: true },
tokens: Array,
profile: {
name: { type: String, default: '' },
gender: { type: String, default: '' },
location: { type: String, default: '' },
website: { type: String, default: '' },
picture: { type: String, default: '' },
phone: {
work: { type: String, default: '' },
home: { type: String, default: '' },
mobile: { type: String, default: '' }
}
},
activity: {
date_established: { type: Date, default: Date.now },
last_logon: { type: Date, default: Date.now },
last_updated: { type: Date }
},
resetPasswordToken: { type: String },
resetPasswordExpires: { type: Date },
verified: { type: Boolean, default: true },
verifyToken: { type: String },
enhancedSecurity: {
enabled: { type: Boolean, default: false },
type: { type: String }, // sms or totp
token: { type: String },
period: { type: Number },
sms: { type: String },
smsExpires: { type: Date }
},
friends: [{
friend: { type: ObjectId, ref: 'User' },
verified: { type: Boolean, default: false }
}]
});
/* (...) some functions that aren't necessary to be shown here */
module.exports = mongoose.model('User', userSchema);
So as you can check I defined Friends inside User like this:
friends: [{
friend: { type: ObjectId, ref: 'User' },
verified: { type: Boolean, default: false }
}]
Now the question is how can I add, edit and delete this array in a Node.js script?
BOTTOMLINE: How can I manipulate arrays that are inside MongoDB Schemas, using Node.js and Mongoose.js? Do I always have to create a Schema function or can I access it directly?
EDIT (13/07/2014): So far I've created a HTTP GET that gives me the array like this:
app.get('/workspace/friends/:userid', passportConf.isAuthenticated, function (req, res) {
User.find({_id: req.params.userid}, function (err, items) {
if (err) {
return (err, null);
}
console.log(items[0].friends);
res.json(items[0].friends);
});
});
But this only returns an array of friendIds, but what if I want to create some sort of '/workspace/friends/:userid/del/:friendid' POST, or add POST. I can't seem to figure out how I can get this done.
You can do something like following
app.get('/workspace/friends/:userid/delete/:friendId', passportConf.isAuthenticated, function (req, res) {
User.findOne({_id: req.params.userid}, function (err, user) {
if (err) {
return (err, null);
}
for (var i = 0; i < user.friends.length; i++) {
if (user.friends[i]._id === req.params.friendId) {
user.friends = user.friends.splice(i,1)
}
}
user.save(function(err, user, numAffected){
if (!err )res.json(user)
res.send('error, couldn\'t save: %s', err)
})
});
});
What it says in mongoose docs is that
"The callback will receive three parameters, err if an error occurred, [model] which is the saved [model], and numberAffected which will be 1 when the document was found and updated in the database, otherwise 0.
The fn callback is optional. If no fn is passed and validation fails, the validation error will be emitted on the connection used to create this model."
If you need to manipulate arrays, you should convert these in objects before.
User.findOne({_id: req.params.userid}, function (err, user) {
if (err) {
return (err, null);
}
var user = user.toObject();
//... your code, an example =>
delete user.friends;
res.json(user);
});
Regards, Nicholls
Related
When I call the method buildCommand, it does not return the property message, but I found out that if I remove some properties out of buildCommand, it works.
This is the method I call
const buildCommand = (commandJSON) => {
return new Command({
prefix: commandJSON.prefix,
command: commandJSON.command,
aliases: commandJSON.aliases,
parameters: commandJSON.parameters,
message: commandJSON.message,
response: commandJSON.response,
commandMedium: commandJSON.commandMedium,
enabled: commandJSON.enabled,
isDefault: commandJSON.isDefault,
permission: commandJSON.permission,
cooldown: commandJSON.cooldown,
});
};
This is how I call the method
const newCommand = buildCommand(commandJSON);
commandJSON looks like this
{ prefix: '!', command: 'laugh', message: 'hahaha' }
UPDATE 2
Here is my whole Command Model
const mongoose = require('mongoose');
const commandSchema = mongoose.Schema({
prefix: {
type: String,
default: '!',
},
command: {
type: String,
required: true,
},
aliases: {
type: Array,
},
parameters: {
type: Array,
},
message: {
type: String,
},
response: {
type: String,
enum: ['chat', 'whisper'],
default: 'chat',
},
commandMedium: {
type: String,
enum: ['offline', 'online', 'both'],
default: 'both',
},
enabled: {
type: Boolean,
default: true,
},
isDefault: {
type: Boolean,
default: false,
},
permission: {
type: String,
enum: ['everyone', 'subscriber', 'vip', 'moderator', 'broadcaster'],
default: 'everyone',
},
cooldown: {
globalCooldown:{type:Boolean, default:false},
globalDuration:{type:Number, default:0},
userDuration:{type:Number,default:0},
}
});
module.exports = mongoose.model('Commands', commandSchema, 'TwitchUsers');
Command is just a Mongoose model. There's nothing async in there, you can (and should) remove the async/await stuff.
You can simply do const newCommand = new Command(commandJSON), job done.
I want to create an experience array in User model with new data, and the problem is that I don't get saved data in exec function so I can push new data in array on frontend. This is what I got so far.
router.post('/:username/experience', function(req, res) {
const username = req.params.username;
User.findOneAndUpdate(
username, {
$push: {
experience: req.body
}
}, {
safe: true,
upsert: true
})
.exec(function (err, data) {
console.log(data, "------>");
});
})
This is my schema for experience, which is called in User model like experience: [ExperienceSchema].
const ExperienceSchema = new Schema({
title: {
type: String,
required: true
},
company: {
type: String,
required: true
},
from: {
type: Date,
},
to: {
type: Date,
},
workingNow: {
type: Boolean,
default: false
},
description: {
type: String
}
}, {
usePushEach: true
})
Since findOneAndUpdate returns the original document (state before update) you need to add new: true to the options in order to get the updated document.
options:
{
safe: true,
upsert: true,
new: true
}
I have a simple help desk app I've been building, where user can make request for site changes. One of the features is being able to see all request made by a specific person, which is working. However on that page I wanted to have something akin to "User's Request" where user is the person's page you are on. However I can't seem to get it to work without some weird issues. If I use:
{{#each request}}
{{user.firstName}}'s Request
{{/each}}
It works but I end up with the header being written as many times as the user has request. However, when I tried:
{{request.user.firstName}}
It returns nothing.
My route is populating user data, so I think I should be able to reference it directly. Here's the route:
// list Request by User
router.get('/user/:userId', (req, res) => {
Request.find({user: req.params.userId})
.populate('user')
.populate('request')
.then(request => {
res.render('request/user', {
request: request,
});
});
});
Here's the schema:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Create Schema
const RequestSchema = new Schema({
title: {
type: String,
required: true,
},
body: {
type: String,
required: true,
},
status: {
type: String,
default: 'new',
},
priority: {
type: String,
default: 'low',
},
project: {
type: String,
default: 'miscellaneous',
},
category: {
type: String,
default: 'change',
category: ['change', 'bug', 'enhancement', 'investigation', 'minor_task', 'major_task', 'question'],
},
organization: {
type: String,
default: 'any',
},
assignedUser: {
type: String,
default: 'venkat',
},
allowComments: {
type: Boolean,
default: true,
},
user: {
type: Schema.Types.ObjectId,
ref: 'users',
},
lastUser: {
type: Schema.Types.ObjectId,
ref: 'users',
},
date: {
type: Date,
default: Date.now,
},
lastUpdate: {
type: Date,
default: Date.now,
},
comments: [{
commentBody: {
type: String,
required: true,
},
commentDate: {
type: Date,
default: Date.now,
},
commentUser: {
type: Schema.Types.ObjectId,
ref: 'users',
},
}],
});
// Create collection and add Schema
mongoose.model('request', RequestSchema);
The rest of the code is at: https://github.com/Abourass/avm_req_desk
If anyone is wondering how, the answer was to add the array identifier to the dot path notation:
<h4>{{request.0.user.firstName}}'s Request</h4>
I've got an Express endpoint that does the following:
router.put('/:appointmentId', async (req, res) => {
try {
let appointment = await Appointment.findOneAndUpdate(
{ _id: req.params.appointmentId },
{
member_id: req.body.appointment.memberId,
client_id: req.body.appointment.clientId,
address_id: req.body.appointment.addressId,
unit_id: req.body.appointment.unitId,
dateAndTime: req.body.appointment.dateAndTime
},
{
new: true,
runValidators: true,
context: 'query'
}
);
res.send(appointment);
} catch (err) {
res.send(err);
}
});
Appointment is a Mongoose model. This model has one of the following path validations:
AppointmentSchema.path("unit_id").validate(async function (unit_id) {
let unit = await Unit.findById(unit_id);
if ((unit === null) || !unit.address_id.equals(this.address_id)) {
return false;
}
return true;
}, "unit_id is not valid");
The Appointment schema looks like this:
var AppointmentSchema = new mongoose.Schema(
{
member_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Member',
required: true
},
client_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Client',
required: true
},
address_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Address',
required: true
},
unit_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Unit',
required: true
},
dateAndTime: {
type: Date,
required: true
}
}
);
When I hit the Express endpoint, this.address_id is undefined and my validations don't work properly. It's my understanding that if I set runValidators to true and context to 'query' that I'd have this access.
What am I doing wrong?
As a side-note, I also notice that Express is returning a 200 status when validation fails. This is curious.
Thanks.
There is my code:
var fileModel = context.models.File,
query = {
_id: context.models.ObjectId("532083358ab1654c0c8b4ced") // TODO: for debug, change after update fix
},
update = {
description: context.data.description,
userId: context.data.userId ?
context.models.ObjectId(context.data.userId) : undefined,
isAdded: true
};
fileModel.update(query, update, { multi: true }, function (err) {
if (err) {
console.log('update');
console.log(err);
context.sendJson({ success: false, err: err });
}
else {
context.sendJson({ success: true });
}
});
There is my Schema:
var fileSchema = new schema({
path: { type: String, required: true, validate: [validateName, 'a path is required'] },
isApproved: { type: Boolean, default: false },
isAdded: { type: Boolean, default: false },
name: { type: String, required: true, validate: [validateName, 'a name is required'] },
description: { type: String },
userId: { type: schema.Types.ObjectId },
updated: { type: Date, default: Date.now },
size: { type: Number }
}, { autoIndex: false });
When I try to update document by id I see this messages in console:
update
[TypeError: Cannot read property '_id' of undefined]
I think problem in
userId: context.data.userId ?
context.models.ObjectId(context.data.userId) : undefined,
But I don't understand how fix it.
I solve this by separate part of my code. But I can't understand what's wrong in my first solution. That's working code:
var fileModel = context.models.File,
query = {
_id: {
$in: context.data.files.map(function (el) {
return context.models.ObjectId(el);
})
}
},
update = {
description: context.data.description,
isAdded: true
};
if (context.data.userId){
update.userId = context.models.ObjectId(context.data.userId);
}
fileModel.update(query, update, { multi: true }, function (err) {
if (err) {
console.log('update');
console.log(err);
context.sendJson({ success: false, err: err });
}
else {
context.sendJson({ success: true });
}
});