i have schema which is nested
so need to validate whole schema, like if it has an extra attributes then throw error
i have tried with strict: 'throw' but that work only for main attributes, not for nested attributes.
const { Schema } = mongoose;
const DatasourceSchema = new Schema({
id: {
type: String,
unique: true,
required: true,
},
display_name: {
type: String,
required: true,
maxlength: 125
},
contact_info: {
type: Schema({
website: {
type: String,
required: false
},
registrar_phone: { type: String },
registrar_email: { type: String },
addresses: [{
type: Schema({
addr1: { type: String, required: false },
addr2: { type: String, required: false },
country: {
name: { type: String, required: true },
a2_code: { type: String, required: true },
}
}),
required: false
}]
}),
required: true,
}
},
{
strict: 'throw',
useNestedStrict: true
});
what i need is if i add any extra KEY (attributes) in object or array at any level that will throw errors
Related
I want to enter data via Postman into an array form using id Collection in the database also how can I write a valid JSON script for the modal for the modal
Add data using id Collection
controller
const addAcademicExperience = async (req, res, next) => {
//const id = req.params.id;
const {AcademicExperience} = req.body;
let academicexperience;
try {
academicexperience = await AcademicExperience.findByIdAndadd(id, {
AcademicExperience
});
await academicexperience.save();
} catch (err) {
console.log(err);
}
if (!academicexperience) {
return res.status(404).json({ message: 'Unable to Add' })
}
return res.status(200).json({academicexperience});
model Schema
Some data is required in an array and some are not
per user per user
To clarify, the site is similar to LinkedIn
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const FacultySchema = new Schema({
Faculty_ID: {
type: Number,
required: true,
unique: true,
},
Name: {
type: String,
required: true,
},
Phone_Number: {
type: String,
required: true,
},
Email: {
type: String,
required: true,
},
AcademicExperience: [{
institution: { type: String, required: true },
rank: { type: String, required: true },
title: { type: String, required: true },
working: { type: Boolean, required: true },
}
],
Certifications:
{
type: String,
require: true,
},
Currentm_embership:
{
type: String,
require: true,
},
Servicea_ctivites:
{
type: String,
require: true,
},
Professional:
{
type: String,
require: true,
},
Education:[ {
degree: { type: String, required: true },
discpilne: { type: String, required: true },
institution: { type: String, required: true },
year: { type: Date, required: true },
}
],
Non_academic_experines:[
{
Company: { type: String, required: true },
title: { type: String, required: true },
working: { type: Boolean, required: true },
Description_of_position: { type: String, required: true },
}
],
Honoers_and_awards:
{
type: String,
require: true,
},
Puplications_and_presentation:
{
type: String,
require: true,
},
});
module.exports = mongoose.model("Faculty", FacultySchema);
Im new using node js , what im trying to do is creating an attribute that contains a list of projects in the userSchema so i can display the projects when the user log in in my electron app
this is my userschema :
var UserSchema = new Schema(
{
username: {
type: String,
unique: true,
required: true,
},
password: {
type: String,
required: true,
},
email: {
type: String,
unique: true,
required: true,
},
nom: {
type: String,
required: true,
},
prenom: {
type: String,
required: true,
},
verif_code: {
type: String,
required: false,
},
}, { timestamps: { createdAt: 'created_at' } });
and this is my projectSchema :
var ProjectSchema = new Schema(
{
description: {
type: String,
},
useremail: {
type: String,
},
imageurl: {
type: String,
},
var UserSchema = new Schema(
{
username: {
type: String,
unique: true,
required: true,
},
password: {
type: String,
required: true,
},
email: {
type: String,
unique: true,
required: true,
},
nom: {
type: String,
required: true,
},
prenom: {
type: String,
required: true,
},
verif_code: {
type: String,
required: false,
},
projects: [{type: Types.ObjectId, ref: 'projects'}],//projects is name of table in database
}, { timestamps: { createdAt: 'created_at' } });
I have a model in Mongoose that looks like this:
const orderLogSchema = mongoose.Schema(
{
removed: { type: String, required: true },
},
{
timestamps: true,
}
)
const orderSchema = mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
orderItems: [
{
name: { type: String, required: true},
qty: { type: Number, required: true},
image: { type: String, required: true},
price: { type: Number, required: true},
product: { type: mongoose.Schema.Types.ObjectId, required: true, ref: 'Product'},
}
],
shippingAddress: {
address: { type: String, required: true },
city: { type: String, required: true },
postalCode: { type: String, required: true },
country: { type: String, required: true },
},
paymentMethod: {
type: String,
required: true,
},
paymentResult: {
id: { type: String },
status: { type: String },
update_time: { type: String },
email_address: { type: String },
},
taxPrice: {
type: Number,
required: true,
default: 0.0
},
shippingPrice: {
type: Number,
required: true,
default: 0.0
},
totalPrice: {
type: Number,
required: true,
default: 0.0
},
isPaid: {
type: Boolean,
required: true,
default: false
},
paidAt: {
type: Date,
},
isDelivered: {
type: Boolean,
required: true,
default: false
},
deliveredAt: {
type: Date,
},
couponCode: {
type: Object,
required: false
},
orderVerifyLog: [orderLogSchema],
}, {
timestamps: true
})
I need to be able to empty out the orderVerifyLog which is populated with the orderLogSchema. I have tried a bunch of things most recently I pulled the order into a variable, and I am able to access orderVerifyLog by using order.orderVerifyLog but I cant figure out how to empty out that orderLogSchema. I tried:
order.orderVerifyLog = []
order.save()
Is there an easy way to work with that "sub" schema and zero it out?
Basically I am pulling the log items from that schema for an order and displaying them on my front-end. Then I was to fire off an action to clears it out so they don't display every time the order is loaded (Only want them to show once). I also tired to loop over each item in the orderVerifyLog and use pull to remove them, but for some reason it always leaves one in there.
The operator $set can't add a new field hide. I thought, I was doing everything according to the mongodb official documentation. Can somebody tall me what I'm doing wrong. Thanks
createClass
.updateMany(
{ classname: req.body.className },
{ $set : {"hide":true}},
{ multi: true, upsert: false },
)
Here is the schema:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const Classes = new Schema({
classname: {
type: String,
required: true,
},
Subject: {
type: String,
required: true,
},
Chapter: {
type: String,
required: true,
},
Topic: {
type: String,
required: true,
},
SubjectimgPath: {
type: String,
},
ChapterimgPath: {
type: String,
},
TopicimgPath: {
type: String,
},
content: {
type: Array,
}
});
Classes.index({ "$**": "text" });
const Createclass = mongoose.model("AllClasses", Classes);
module.exports = Createclass;
Try this :
const Classes = new Schema({
classname: {
type: String,
required: true,
},
Subject: {
type: String,
required: true,
},
Chapter: {
type: String,
required: true,
},
Topic: {
type: String,
required: true,
},
SubjectimgPath: {
type: String,
},
ChapterimgPath: {
type: String,
},
TopicimgPath: {
type: String,
},
content: {
type: Array,
}
}, {strict :false} );
I'm getting a cyclic dependency exception whereas I don't find any cyclic possibility in the very simple code at this point. I'm expecting it is happening due to population between two mongoose models but as said here : mongoose - possible circular dependency? this shouldn't produce this kind of error so I don't understand.
model\customer.js
var mongoose = require('mongoose'), Schema = mongoose.Schema;
var customerSchema = new Schema({
_siretCustomer:
{ type: String, required: true, minlength: 15, maxlength: 15, index: {unique: true} },
fileNumberCustomer:
{ type: String, required: true, minlength: 6, maxlength: 6 },
companyTypeCustomer:
{ type: String, required: true },
companyNameCustomer:
{ type: String, required: true },
contactName1Customer:
{ type: String, required: false, default: null },
contactName2Customer:
{ type: String, required: false, default: null },
landlineContact1Customer:
{ type: String, required: false, default: null },
landlineContact2Customer:
{ type: String, required: false, default: null },
emailContactCustomer:
{ type: String, required: false, default: null },
obligationCommentCustomer:
{ type: String, required: false, default: null },
taskCommentCustomer:
{ type: String, required: false, default: null },
creationDateCustomer:
{ type: Date, default: Date.now },
userCustomer:
[{ type: Schema.Types.ObjectId, ref: 'User', required: true }]
}, { collection: 'customer' });
module.exports = mongoose.model('Customer', customerSchema, 'customer');`
model\user.js
var mongoose = require('mongoose'), Schema = mongoose.Schema;
var userSchema = new Schema({
_trigramUser:
{ type: String, required: true, index: { unique: true } },
lastnameUser:
{ type: String, required: true },
firstnameUser:
{ type: String, required: true },
privilegeUser:
{ type: String, required: true, enum: ['Accountant', 'Social operator'] },
dateCreationUser:
{ type: Date, default: Date.now },
passwordUser:
{ type: String, required: true },
customersUser:
[{ type: String, ref: 'Customer'}]
}, { collection: 'user'});
module.exports = mongoose.model('User', userSchema, 'user');
controller\session\customerController.js
exports.readCustomer = (req, res) => {
database.getConnection();
customer.find(req).populate('userCustomer').exec((err, customer) => {
if (err) return res.status(500).json(err.stack);
res.status(200).json(customer);
});
};