Let's say that I have a nested Schema (invitation) in another schema (item) and when trying to save an invitation object, I want to check if the parent property 'enabled' from the item schema is set to true or false before allowing the person to save the invite object to the invitations array. Obviously, this.enabled doesn't work as it's trying to get it off of the invitationSchema, which doesn't exist. How would one get the 'enabled' property on the parent schema to validate?
Any thoughts? Thanks for any help!
var validateType = function(v) {
if (v === 'whateverCheck') {
return this.enabled || false; <== this doesn't work
} else {
return true;
}
};
var invitationSchema = new Schema({
name: { type: String },
type: {
type: String,
validate: [validateType, 'error message.']
}
});
var itemSchema = new Schema({
title: { type: String },
description: { type: String },
enabled: {type: Boolean}, <== trying to access this here
invitations: { type: [ invitationSchema ] },
});
var ItemModel = mongoose.model('Item', itemSchema, 'items');
var InvitationModel = mongoose.model('Invitation', invitationSchema);
The parent of an embedded document is accessible from an embedded doc model instance by calling instance.parent();. So you can do this from any Mongoose middleware like a validator or a hook.
In your case you can do :
var validateType = function(v) {
if (v === 'whateverCheck') {
return this.parent().enabled || false; // <== this does work
} else {
return true;
}
};
Related
Even though the question have been asked numerous time none of the answers have any idea to help me .
This is my mongoose Schema
const mongoose = require('mongoose')
const { Schema } = mongoose;
const recipeSchema = new Schema({
name: { type: String, required: true },
description: { type: String, required: true },
imagePath: { type: String, required: true },
ingredients:[
{
name:{type:String, required:true},
amount:{type:Number,required:true }
}
]
})
module.exports = mongoose.model("Recipe",recipeSchema);
what i need is to get the data from angular and store it to my database using node
const Recipe = require('../models/recipe.model');
const recipeCtrl={};
recipeCtrl.CreateRecipeServer =async(req, res, next)=>{
if(!req.file) {
return res.status(500).send({ message: 'Upload fail'});
}
else {
let ingredientArray=new Array()
ingredientArray.push(req.body.ingredients)
req.body.imageUrl = 'http://192.168.0.7:3000/images/' + req.file.filename;
const recipe=new Recipe({
name:req.body.name,
description:req.body.description,
imagePath:req.body.imageUrl,
ingredients:[
{
name:ingredientArray,
amount:ingredientArray }
]
});
await recipe.save();
}
Everything except the ingredients array works perfectly/as i require.
I am getting the ingredients as an array from formdata so it have to be JSON.stringfied inorder to append with the form. So what i am getting at backend is string . eg
**[{"name":"dasdasd","amount":2},{"name":"fsfsd","amount":2},{"name":"sdfsdgd","amount":3}]**
this is a string. Any ideas on how to convert it and store to database
use JSON.parse and choose first element of that
JSON.parse(data)[0]
I'm trying to implement an autoicremental user_key field. Looking on this site I came across two questions relevant for my problem but I don't clearly understand what I should do. This is the main one
I have two Mongoose models, this is my ProductsCounterModel.js
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var Counter = new Schema({
_id: {type: String, required: true},
sequence_value: {type: Number, default: 0}
});
module.exports = mongoose.model('products_counter', Counter);
and this is the Mongoose model where I try to implement the auto-increment field:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var products_counter = require('./ProductsCounterModel.js');
var HistoricalProduct = new Schema({
product_key: { type: Number },
class: { type: String },
brand: { type: String },
model: { type: String },
description: { type: String }
});
HistoricalProduct.pre("save", function (next) {
console.log("first console log:",products_counter);
var doc = this;
products_counter.findOneAndUpdate(
{ "_id": "product_key" },
{ "$inc": { "sequence_value": 1 } },
function(error, products_counter) {
if(error) return next(error);
console.log("second console log",products_counter);
doc.product_key = products_counter.sequence_value;
next();
});
});
module.exports = mongoose.model('HistoricalProduct', HistoricalProduct);
Following the steps provided in the above SO answer I created the collection products_counter and inserted one document.
The thing is that I'm getting this error when I try to insert a new product:
"TypeError: Cannot read property 'sequence_value' of null"
This are the outputs of the above console logs.
first console log output:
function model (doc, fields, skipId) {
if (!(this instanceof model))
return new model(doc, fields, skipId);
Model.call(this, doc, fields, skipId);
}
second console log:
Null
can you see what I'm doing wrong?
You can run following line in your middleware:
console.log(products_counter.collection.collectionName);
that line will print products_counters while you expect that your code will hit products_counter. According to the docs:
Mongoose by default produces a collection name by passing the model name to the utils.toCollectionName method. This method pluralizes the name. Set this option if you need a different name for your collection.
So you should either rename collection products_counter to products_counters or explicitly configure collection name in your schema definition:
var Counter = new Schema({
_id: {type: String, required: true},
sequence_value: {type: Number, default: 0}
}, { collection: "products_counter" });
I have a simple model, which is:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var citySchema = new Schema({
name: { type: String, required: true },
state: { type: Schema.Types.ObjectId, ref: 'State' }
});
module.exports = mongoose.model('City', citySchema);
Only i access a route, asking to insert a city, giving as post params
POST: {
name: 'My City',
state: 'SE' // Stands for Some State
}
I know the type of the state property is not correct, but in my logic i do:
var newCity = new City(req.body);
if (typeof req.body.state !== 'undefined' && req.body.state.length == 2) {
State.findOne({uf: req.body.state.toUpperCase()}, function(err, foundState) {
if (err) { res.send({status: 500, message: 'Could not find the required state'}); return; }
newCity.state = foundState._id;
newCity.set('state', foundState._id);
return;
});
}
But, once i do a res.send(newCity), to check out the newCity variable properties, it prints:
{
"name": "Balneário Camború",
"_id": "570ff2944c6bd6df4e8e76e8"
}
And if i try to save it, i get the following error:
ValidationError: CastError: Cast to ObjectID failed for value \"SE\" at path \"state\""
So, i'm quite confused, because when the Model is created using the req.body properties, it does not list the state property, even if i set it later in the code, but when i try to save the City, it throw an error of mistype.
What could be causing this, and how should i procede?
I have been trying this with the built in inheritance features of mongoose (rather than the extend plugin) but haven't been having much luck so far. This is a simplified example of code I am trying to use which exhibits the same problem. This is based on an expanded version of the mongoose documentation for schema inheritance using discriminators - http://mongoosejs.com/docs/api.html#model_Model.discriminator
var util = require('util');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/problem');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
function BaseSchema() {
Schema.apply(this, arguments);
this.add({
name: String,
createdAt: Date
});
}
util.inherits(BaseSchema, Schema);
var BossStatusSchema = new Schema({
status: String
});
var BossStatus = mongoose.model('BossStatus', BossStatusSchema);
var PersonSchema = new BaseSchema();
var Person = mongoose.model('Person', PersonSchema);
var BossSchema = new BaseSchema({
department: String,
bossStatus: {
type: ObjectId,
ref: 'BossStatus'
}
});
var Boss = Person.discriminator('Boss', BossSchema);
Example code to add the documents:
var superBoss = new BossStatus({
status: 'super'
});
var normalBoss = new BossStatus({
status: 'normal'
});
var andy = new Person({
name: 'Andy'
});
var billy = new Boss({
name: 'Billy',
bossStatus: superBoss._id
});
var callback = function(err, result) {
console.dir(err);
console.dir(result);
};
superBoss.save(callback);
normalBoss.save(callback);
andy.save(callback);
billy.save(callback);
So when finding a record without populate:
Person
.findOne({
name: 'Billy'
})
.exec(callback);
The result is as expected, the bossStatus refers to an _id from the bossstatuses collection:
null
{ name: 'Billy',
bossStatus: 52a20ab0185a7f4530000001,
_id: 52a20ab0185a7f4530000004,
__v: 0,
__t: 'Boss' }
When adding the populate call:
Person
.findOne({
name: 'Billy'
})
.populate('bossStatus')
.exec(callback);
The resulting bossStatus property of the Person result is null:
null
{ name: 'Billy',
bossStatus: null,
_id: 52a20ab0185a7f4530000004,
__v: 0,
__t: 'Boss' }
EDIT:
Ok I've just put together what is probably a better example of what I'm trying to achieve, the schema structure lends itself more to a relational DB but hopefully makes the problem clearer.
var util = require('util');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/problem');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
function BaseSchema() {
Schema.apply(this, arguments);
this.add({
name: {
type: String,
unique: true,
required: true
}
});
}
util.inherits(BaseSchema, Schema);
var DeviceSchema = new BaseSchema();
var LocalDeviceSchema = new BaseSchema({
driver: {
type: ObjectId,
ref: 'Driver'
}
});
var RemoteDeviceSchema = new BaseSchema({
networkAddress: {
type: ObjectId,
ref: 'NetworkAddress'
}
});
var DriverSchema = new Schema({
name: {
type: String,
unique: true,
required: true
}
});
var NetworkHostSchema = new Schema({
host: {
type: String,
unique: true,
required: true
}
});
var NetworkAddressSchema = new Schema({
networkHost: {
type: ObjectId,
ref: 'NetworkHost'
},
port: {
type: Number,
min: 1,
max: 65535
}
});
var Driver = mongoose.model('Driver', DriverSchema);
var NetworkHost = mongoose.model('NetworkHost', NetworkHostSchema);
var NetworkAddress = mongoose.model('NetworkAddress', NetworkAddressSchema);
var Device = mongoose.model('Device', DeviceSchema);
var LocalDevice = Device.discriminator('LocalDevice', LocalDeviceSchema);
var RemoteDevice = Device.discriminator('RemoteDevice', RemoteDeviceSchema);
var networkHost = new NetworkHost({
host: '192.168.2.1'
});
var networkAddress = new NetworkAddress({
networkHost: networkHost._id,
port: 3000
});
var remoteDevice = new RemoteDevice({
name: 'myRemoteDevice',
networkAddress: networkAddress._id
});
var driver = new Driver({
name: 'ftdi'
});
var localDevice = new LocalDevice({
name: 'myLocalDevice',
driver: driver._id
});
var callback = function(err, result) {
if(err) {
console.log(err);
}
console.dir(result);
};
/*
// Uncomment to save documents
networkHost.save(function() {
networkAddress.save(function() {
remoteDevice.save(callback);
});
});
driver.save(function() {
localDevice.save(callback);
});
*/
var deviceCallback = function(err, device) {
if(err) {
console.log(err);
}
switch(device.__t) {
case 'LocalDevice':
console.log('Would create a local device instance passing populated result');
break;
case 'RemoteDevice':
console.log('Would create a remote device instance passing populated result');
break;
}
};
Device
.findOne({name: 'myLocalDevice'})
.populate('driver')
.exec(deviceCallback);
The LocalDevice and RemoteDevice schemas could (and probably would) include other differences..
The switch would for example use a DeviceFactory or something to create the instances. My thinking was it should be possible to search the devices table for a device by 'name' and populate the collection references (if this is the correct terminology?) without having to specify the collection to search in - this was my understanding of the point of schema inheritance - or have I completely misunderstood?
Thanks for replies so far!
You are looking for a Boss, not a Person:
Boss
.findOne({
name: 'Billy'
})
.populate('bossStatus')
.exec(callback);
Looks like a bug. With debugging active, this is what's being shown for the population query:
Mongoose: people.findOne({ name: 'Billy' }) { fields: undefined }
Mongoose: people.find({ _id: { '$in': [ ObjectId("52a221ee639cc03d71000001") ] } }) { fields: undefined }
(the ObjectId shown is the one stored in bossStatus)
So Mongoose is querying the wrong collection (people instead of bossstatuses).
As #regretoverflow pointed out, if you're looking for a boss, use the Boss model and not the Person model.
If you do want to populate bossStatus through the Person model, you can explicitly state a model that needs to be searched for population:
.populate({
path : 'bossStatus',
model : 'BossStatus'
})
// or shorter but less clear:
// .populate('bossStatus', {}, 'BossStatus')
EDIT: (with your Device examples)
driver is part of LocalDeviceSchema, but you're querying the Device model, which has no notion of what driver is and populating driver within the context of a Device instance doesn't make sense to Mongoose.
Another possibility for populating each instance is to do it after you retrieved the document. You already have the deviceCallback function, and this will probably work:
var deviceCallback = function(err, device) {
if(err) {
console.log(err);
}
switch(device.__t) { // or `device.constructor.modelName`
case 'LocalDevice':
device.populate('driver', ...);
break;
case 'RemoteDevice':
device.populate('networkAddress', ...);
break;
}
};
The reason is that the document is already cast into the correct model there, something that apparently doesn't happen when you chain populate with the find.
Mongoose seems to default to make all fields not required. Is there any way to make all the fields required without changing each of:
Dimension = mongoose.Schema(
name: String
value: String
)
to
Dimension = mongoose.Schema(
name:
type: String
required: true
value:
type: String
required: true
)
It'll get really ugly since I have a lot of these.
You could do something like:
var schema = {
name: { type: String},
value: { type: String}
};
var requiredAttrs = ['name', 'value'];
for (attr in requiredAttrs) { schema[attr].required = true; }
var Dimension = mongoose.schema(schema);
or for all attrs (using underscore, which is awesome):
var schema = {
name: { type: String},
value: { type: String}
};
_.each(_.keys(schema), function (attr) { schema[attr].required = true; });
var Dimension = mongoose.schema(schema);
I ended up doing this:
r_string =
type: String
required: true
r_number =
type: Number
required: true
and on for the other data types.
All fields properties are in schema.paths[attribute] or schema.path(attribute);
One proper way to go : define when a field is NOT required,
Schema = mongoose.Schema;
var Myschema = new Schema({
name : { type:String },
type : { type:String, required:false }
})
and make them all required by default :
function AllFieldsRequiredByDefautlt(schema) {
for (var i in schema.paths) {
var attribute = schema.paths[i]
if (attribute.isRequired == undefined) {
attribute.required(true);
}
}
}
AllFieldsRequiredByDefautlt(Myschema)
The underscore way :
_=require('underscore')
_.each(_.keys(schema.paths), function (attr) {
if (schema.path(attr).isRequired == undefined) {
schema.path(attr).required(true);
}
})
Test it :
MyTable = mongoose.model('Myschema', Myschema);
t = new MyTable()
t.save()
Well you could write a mongoose schema plugin function that walked the schema object and adjusted it to make each field required. Then you'd just need 1 line per schema: Dimension.plugin(allRequired).
Mongoose didn't provide the method of setting all fields, but you could do it recursively.
Like Peter mentioned, you could pluginize it in order to reuse the code.
Recursively setting:
// game.model.js
var fields = require('./fields');
var Game = new Schema({ ... });
for(var p in Game.paths){
Game.path(p).required(true);
}
Pluginized:
// fields.js
module.exports = function (schema, options) {
if (options && options.required) {
for(var p in schema.paths){
schema.path(p).required(true);
}
}
}
// game.model.js
var fields = require('./fields');
var Game = new Schema({ ... });
Game.plugin(fields, { required: true });
I'm not sure if there's an easier way to do it in Mongoose, but I would do the following in your IDE/editor:
List out your fields as you would normally:
Dimension = mongoose.Schema(
name: String
value: String
)
Then do a find and replace on String and replace it with {type: String, required: true}, Giving you:
Dimension = mongoose.Schema(
name: {type: String, required: true},
value: {type: String, required: true},
)
Then do the same for Number and other types.
Building on the previous answers, the module below will make fields required by default. The previous answers did not recurse nested objects/arrays.
Usage:
const rSchema = require("rschema");
var mySchema = new rSchema({
request:{
key:String,
value:String
},
responses:[{
key:String,
value:String
}]
});
Node module:
const Schema = require("mongoose").Schema;
//Extends Mongoose Schema to require all fields by default
module.exports = function(data){
//Recursive
var makeRequired = function(schema){
for (var i in schema.paths) {
var attribute = schema.paths[i];
if (attribute.isRequired == undefined) {
attribute.required(true);
}
if (attribute.schema){
makeRequired(attribute.schema);
}
}
};
var schema = new Schema(data);
makeRequired(schema);
return schema;
};