How do I access the data in the .pre('validation') hook? - javascript

I am having trouble with performing operations during the pre('validate') hook. I need to do some prevalidation (making sure at least one of 2 different fields is populated, but not necessarily both, for example).
const AccessorySchema = new Schema({
accessory: {
type: String,
required: true,
},
category: {
type: String,
required: true,
enum: [
'Offense',
'Defence',
'Miscellaneous'
]
},
space: {
type: Number,
required: true,
validate: {
validator: Number.isInteger,
message: 'Space must be an integer'
}
},
priceFixed: {
type: Number,
required: false,
validate: {
validator: Number.isInteger,
message: 'Fixed Price must be an integer'
}
},
priceMultiplier: {
type: [Schema.Types.Mixed],
required: false
},
weightFixed: {
type: Number,
required: false,
validate: {
validator: Number.isInteger,
message: 'Fixed Weight must be an integer'
}
},
weightMultiplier: {
type: [Schema.Types.Mixed],
required: false
},
vehicles: {
type: [String],
required: true,
enum: ["car","cycle"]
}
});
AccessorySchema.pre('validate', (next) => {
console.log(this);
next();
});
And I send it this object :
{
accessory: "some name",
category: "Miscellaneous",
priceMultiplier: [3,5],
weightMultiplier: [3,5],
space: 0,
vehicles: ["car"]
}
this logs {} and populates the mongo DB. But I can't check any of the properties in pre validation.
mongoose version is ^4.7.7, nodejs 6.10.2, mongodb version is 3.2.9
How can I access the data in the pre validation hook?

do not use arrow function, it doesn't bind the context.
Change your code to below
AccessorySchema.pre('validate', function(next){
console.log(this);
next();
});

Related

MongoDB Create New Document that references another Collection's Document IDs

I am trying to create a new document in the Form collection. This document references many FormSection documents. Here are the Schemas:
const FormSchema = new Schema({
title: {
type: String,
required: true,
unique: true
},
description: {
type: String,
required: true,
unique: true
},
sections: [{
type: FormSectionDetails
}],
createdDate: {
type: String,
required: false,
unique: true
},
lastEdited: {
type: String,
required: false,
unique: true
}
});
const FormSectionDetails = new Schema({
section: {
type: Schema.Types.ObjectId,
ref: 'FormSection',
required: true
},
position: {
type: Number,
required: true
}
});
const FormSectionSchema = new Schema({
name: {
type: String,
required: true,
unique: true
},
display: {
type: String,
required: true,
},
category: {
type: String,
required: true
},
...
});
let FormSection;
try {
FormSection = mongoose.connection.model('FormSection');
} catch (e) {
FormSection = mongoose.model('FormSection', FormSectionSchema);
}
However, when I try to add a new document to the Forms collection, I get an error:
Document being inserted:
formData = {
"title": "Returning Members",
"description": "Returning Members",
"sections":
[{
"section": "6292c0fbd4837faca1d85d4d",
"position": 1
},
{
"section": "6292c0fbd4837faca1d85d4e",
"position": 2
},
...
}
Code being run:
formdata.sections.map(s => {
return {
...s,
section: ObjectId(s.section),
}}
);
return await FormSection.create(formdata);
Error message:
ValidationError: category: Path `category` is required., display: Path `display` is required.````
Seems like it is trying to create a new FormSection document. I don't want it to create a new FormSection document. I just want it to reference existing FormSection documents using the Object IDs I specified.
The Issue seems to be with how you declare the section field in the FormSchema. Try this:
const FormSchema = new Schema({
title: {
type: String,
required: true,
unique: true
},
description: {
type: String,
required: true,
unique: true
},
sections: [{
type: ObjectId,
ref: 'FormSectionDetails',
required: true,
}],
createdDate: {
type: String,
required: false,
unique: true
},
lastEdited: {
type: String,
required: false,
unique: true
}
});
This would just store the _ids of the existing FormSectionDetails
It turns out I was inserting the document into the wrong collection. Instead of the code snippet:
return await FormSection.create(formdata);
It should actually be:
return await Form.create(formdata);
The error message should have been a more obvious hint for me as to what the problem was.

Mongoose: Check if value exists in string array

I am querying to get all articles that have a specific source name and category.
I have an article schema that looks like below.
The issue I'm facing is that, it fetches the articles with the correct source name, but it doesn't fetch for the right category. For example, if source is "cnn" and category is "sports", it fetches all articles from CNN correctly, but not with correct categories (the articles may have a categories of ['politics', 'culture'] with no sports)
const article_schema = new mongoose.Schema({
title: {
type: String,
required: true
},
thumbnail_url: {
type: String,
required: true
},
summary: {
type: String,
required: true
},
text: {
type: String,
required: true
},
link: {
type: String,
required: true
},
publish_date: {
type: String,
required: true
},
source: {
name: {
type: String,
required: true
},
name_slug: {
type: String,
required: true
},
link: {
type: String,
required: true
},
display_picture_url: {
type: String,
required: true
},
biography: {
type: String,
required: true
},
tags: {
type: [String],
required: true
},
rssFeeds: [{
url: {
type: String,
required: true
},
categories: {
type: [String],
required: true
}
}]
}
});
let getNews = async (req, res) => {
const { sort, filter, current_page, limit, category_slug } = req.body;
let articles = await Article.findOne({ 'source.name_slug': filter, categories: category_slug });
res.setHeader('Content-Type', 'application/json');
res.status(200).send({ articles });
}

How to sustainably organise user schema in mongoose

I have a user which should have the following fields and currently has following schema:
const UserSchema = new Schema(
{
email: {
type: String,
required: true,
index: { unique: true },
lowercase: true,
},
isVerified: { type: Boolean, default: false }, // whether user has confirmed his email
name: { type: String, required: false },
password: { type: String, required: true, minLength: 6 }, // object with next two included?
passwordResetExpires: Date,
passwordResetToken: String,
roles: [{ type: 'String' }], // Array of strings?
username: { type: String, required: false },
token: [{ type: String, required: false }], // used to send verification token via email
},
{ timestamps: true },
);
So yes, what is the world's default standard for organising user schemas. This schema's fields are pretty common, right?

What's the difference between document.property and document.get('property')?

I have a mongoose document that has timestamps option enabled. I want to make decisions based on this timestamps but I noticed something weird according to my understanding.
I tried to get those values the traditional way (document.createdAt) but that returns undefined. But if I use document.get('createdAt') the value comes as in the database. The docs don't say anything about this. My question is: ¿Why timestamps behave this way?
Edit
The schema I'm using has an array of embedded schemas:
const Customer = new mongoose.Schema({
roles: {
type: [{
type: String,
enum: 'app b2b iot'.split(' '),
}],
default: 'app',
set: (value = []) => (value.includes('app')
? value
: value.concat('app')),
},
email: {
address: {
type: String,
trim: true,
lowercase: true,
set(email) {
this._previousEmail = this.email.address
return email
},
},
verified: {
type: Boolean,
},
token: String,
},
nickname: {
type: String,
trim: true,
},
recoveryToken: String,
gender: String,
birthday: String,
lastLogin: Date,
isAnonymous: {
type: Boolean,
default: false,
},
devices: [Device],
});
Device schema:
const Device = new mongoose.Schema({
customer: {
type: ObjectId,
ref: 'Customer',
required: true,
},
handle: {
type: String,
},
platform: {
type: String,
required: true,
set: toLowerCase,
},
info: Mixed,
smartFilterTags: [{
type: String,
}],
paidUntil: Date,
nh: {
tier: String,
_id: {
type: ObjectId,
},
location: {
type: {
type: String,
enum: ['Point'],
default: 'Point',
},
coordinates: [{
type: Number,
}],
},
})
I have a base plugin that apply when I compile models:
function basePlugin(schema) {
schema.add({
archivedAt: Date,
})
schema.set('timestamps', true)
schema.set('toJSON', {
virtuals: true,
})
schema.set('toObject', {
virtuals: true,
})
}

Aggregation query with $match works in MongoDB Compass but not on my Node.js app

I have a Booking model and an Event model. I'm attempting to query and check if there are any bookings that have a specific Event._id and User._id already, as to stop the creation of a duplicate Booking for that user and event. The aggregation query works on MongoDB compass, however it's only giving me an empty array when I try the query in the Node.js app
Models
Booking
const BookingSchema = new Schema({
amount: {
type: Number,
required: 'Please supply a number of people',
},
event: {
type: mongoose.Schema.ObjectId,
ref: 'Event',
required: 'Must give an event!',
},
booker: {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: 'You must supply the booker!',
},
confirmed: {
type: Boolean,
default: false,
},
});
Event
const eventSchema = new Schema(
{
title: {
type: String,
trim: true,
required: 'You must add an event name!',
},
description: {
type: String,
trim: true,
},
slug: String,
created: {
type: Date,
default: Date.now,
},
date: {
type: Date,
min: Date.now,
required: 'Please enter a valid event Date!',
},
minCapacity: {
type: Number,
required: 'Please enter a correct min capacity for your event!',
},
maxCapacity: {
type: Number,
required: 'Please enter a correct max capacity for your event!',
},
price: Number,
location: {
type: {
type: String,
default: 'Point',
},
coordinates: [
{
type: Number,
required: 'You must supply coords!',
},
],
address: {
type: String,
required: 'Please enter a valid address!',
},
},
photo: String,
author: {
type: Schema.ObjectId,
ref: 'User',
required: 'You must supply an author!',
},
available: Boolean,
// attendees: [User], you can do through virtuals
},
{
toJSON: { virtuals: true },
toObject: { virtuals: true },
}
);
eventSchema.virtual('bookings', {
ref: 'Booking', // what model is linked?
localField: '_id', //what field on model
foreignField: 'event', //which field on Booking?
});
module.exports = mongoose.model('Event', eventSchema);
Query
exports.createBooking = async (req, res) => {
req.body.booker = req.user._id;
req.body.event = req.params.id;
const bookings = await Booking.aggregate(
[
{
$match: {
event: req.params.id,
},
},
{ $count: 'bookings' },
],
);
return res.json(bookings);
};
Thank you in Advanced! And if there's any other information you'd like, please just let me know.
You have to cast your id from String to ObjectId
const ObjectID = require('mongodb').ObjectID
[
{ "$match": {
"event": ObjectId(req.params.id),
}},
{ "$count": "bookings" },
]

Categories

Resources