Mongoose validation failed, without giving the field-name with the issue - node.js

When I try to save my "Rating" schema, it gives the error
error: ValidationError: Rating validation failed
without giving the field name with the problem, neither the kind of issue (if it's about a casting, a null or missing value, or something else..)
In my schema I don't have any field "required", neither "index:true"... I just specify the types in this way:
{
ex_string_field : {type:String},
ex_number_field : {type:Number},
ex_array_field : [
{
sub_field_string : {type:String}
}
],
ex_object_field : {
sub_field_string : {type:String}
}
}
How can I find which is the field rising the error? I have many fields. Actually, without any required field, it should be pretty easy to validate an object. I guessed that if a field is not compliant, it's just ignored....
This is my system:
"mongoose": "^4.11.12",
"node" : "v8.1.3",
"npm" : "5.3.0"
And this is the full error stack track:
2017-12-5 - error: ValidationError: Rating validation failed
at ValidationError (/Volumes/HD Daniele/Sites/cashinvoice/server/node_modules/mongoose/lib/error/validation.js:28:11)
at model.Document.invalidate (/Volumes/HD Daniele/Sites/cashinvoice/server/node_modules/mongoose/lib/document.js:1658:32)
at model.Document.$set (/Volumes/HD Daniele/Sites/cashinvoice/server/node_modules/mongoose/lib/document.js:760:10)
at model._handleIndex (/Volumes/HD Daniele/Sites/cashinvoice/server/node_modules/mongoose/lib/document.js:590:14)
at model.Document.$set (/Volumes/HD Daniele/Sites/cashinvoice/server/node_modules/mongoose/lib/document.js:550:24)
at model._handleIndex (/Volumes/HD Daniele/Sites/cashinvoice/server/node_modules/mongoose/lib/document.js:574:12)
at model.Document.$set (/Volumes/HD Daniele/Sites/cashinvoice/server/node_modules/mongoose/lib/document.js:550:24)
at model.Document (/Volumes/HD Daniele/Sites/cashinvoice/server/node_modules/mongoose/lib/document.js:77:12)
at model.Model (/Volumes/HD Daniele/Sites/cashinvoice/server/node_modules/mongoose/lib/model.js:55:12)
at new model (/Volumes/HD Daniele/Sites/cashinvoice/server/node_modules/mongoose/lib/model.js:3885:13)
at /Volumes/HD Daniele/Sites/cashinvoice/server/server/routes/admin/admin-utils.js:370:13
at _fulfilled (/Volumes/HD Daniele/Sites/cashinvoice/server/node_modules/q/q.js:854:54)
at self.promiseDispatch.done (/Volumes/HD Daniele/Sites/cashinvoice/server/node_modules/q/q.js:883:30)
at Promise.promise.promiseDispatch (/Volumes/HD Daniele/Sites/cashinvoice/server/node_modules/q/q.js:816:13)
at /Volumes/HD Daniele/Sites/cashinvoice/server/node_modules/q/q.js:624:44
at runSingle (/Volumes/HD Daniele/Sites/cashinvoice/server/node_modules/q/q.js:137:13)
================== UPDATE ====================
After some testing, I ended up that the error is the complex field 'main_info'. In the specific, 2 types were swapped (a number as String, and a String as Number). They were defined as follow:
this.schema = new Schema({
main_info :
{
rea : {type:String},
name : {type:Number},
}
});
This is an example of passed data that rise the Validation Error:
main_info :
{
"rea": 649087,
"name": "NUTIS S.R.L."
}
Anyway I think that it's crazy manual parse the whole data to find which is the problem. Does Mongoose really not provide any further info about the validation fail?

Related

I got error while validating field in mongodb, nodejs

These are the errors I got from backend while validating in model and received in 'err' variable. If I console.log(err), then it shows following errors.
Error [ValidationError]: employees validation failed: fullName: This field is required from model, email: This field is required from model
at ValidationError.inspect (D:\Programming\MERN\CRUD\node_modules\mongoose\lib\error\validation.js:61:24)
at formatValue (internal/util/inspect.js:703:31)
at inspect (internal/util/inspect.js:272:10)
at formatWithOptions (internal/util/inspect.js:1887:40)
at Object.Console.<computed> (internal/console/constructor.js:284:10)
at Object.log (internal/console/constructor.js:294:61)
at handleValidationError (D:\Programming\MERN\CRUD\routes\/employeeRoute.js:46:13)
at D:\Programming\MERN\CRUD\routes\/employeeRoute.js:33:17
at D:\Programming\MERN\CRUD\node_modules\mongoose\lib\model.js:4915:16
at D:\Programming\MERN\CRUD\node_modules\mongoose\lib\helpers\promiseOrCallback.js:16:11
at D:\Programming\MERN\CRUD\node_modules\mongoose\lib\model.js:4938:21
at D:\Programming\MERN\CRUD\node_modules\mongoose\lib\model.js:492:16
at D:\Programming\MERN\CRUD\node_modules\kareem\index.js:246:48
at next (D:\Programming\MERN\CRUD\node_modules\kareem\index.js:167:27)
at next (D:\Programming\MERN\CRUD\node_modules\kareem\index.js:169:9)
at Kareem.execPost (D:\Programming\MERN\CRUD\node_modules\kareem\index.js:217:3) {
errors: {
fullName: MongooseError [ValidatorError]: This field is required from model
at new ValidatorError (D:\Programming\MERN\CRUD\node_modules\mongoose\lib\error\validator.js:29:11)
at validate (D:\Programming\MERN\CRUD\node_modules\mongoose\lib\schematype.js:1178:13)
at D:\Programming\MERN\CRUD\node_modules\mongoose\lib\schematype.js:1161:7
at Array.forEach (<anonymous>)
at SchemaString.SchemaType.doValidate (D:\Programming\MERN\CRUD\node_modules\mongoose\lib\schematype.js:1106:14)
at D:\Programming\MERN\CRUD\node_modules\mongoose\lib\document.js:2378:18
at processTicksAndRejections (internal/process/task_queues.js:79:11) {
properties: [Object],
kind: 'required',
path: 'fullName',
value: '',
reason: undefined,
[Symbol(mongoose:validatorError)]: true
},
email: MongooseError [ValidatorError]: This field is required from model
at new ValidatorError (D:\Programming\MERN\CRUD\node_modules\mongoose\lib\error\validator.js:29:11)
at validate (D:\Programming\MERN\CRUD\node_modules\mongoose\lib\schematype.js:1178:13)
at D:\Programming\MERN\CRUD\node_modules\mongoose\lib\schematype.js:1161:7
at Array.forEach (<anonymous>)
at SchemaString.SchemaType.doValidate (D:\Programming\MERN\CRUD\node_modules\mongoose\lib\schematype.js:1106:14)
at D:\Programming\MERN\CRUD\node_modules\mongoose\lib\document.js:2378:18
at processTicksAndRejections (internal/process/task_queues.js:79:11) {
properties: [Object],
kind: 'required',
path: 'email',
value: '',
reason: undefined,
[Symbol(mongoose:validatorError)]: true
}
},
_message: 'employees validation failed'
}
Now if I console.log('err.errors.fullName.message'). It works perfectly.
But if there are more keys, it's obvious that we need to use loop. So if I try to iterate through keys of object as follow, then it throws following error.
for(field in err.errors){
.......
.......
}
Error is:
ReferenceError: field is not defined
What's the problem here ?
First of all your backend should not give this type of error response as you mentioned in the above. This must be like a JSON response of errors. e.g
{ success: false, errors: [{fullName: "fullName is required"}] }
Coming to your question. If you want to show error properly you can loop through the error which you can pic by the err.errors
eg err.errors.forEach(error => console.log(error) )
But again this is a very wrong way to throw the validation error. To validate any data you can use joi, request-validator, validatorjs, etc. Which will give you a perfect error response as you want? Why I am giving stress on this because you are exposing entire directory, files, model name, etc.
I hope this may help you to solve your problem

Error while dropping the index in Cassandra

While creating the new index in cassandra by mistake given same directory path(directory_path) for both the indexes which is might be giving error while dropping the indexes. And able to delete other indexes which is pointing to unique different directory paths. Please share your thoughts that how we can delete the new index which was pointed to same directory path of existing index.
New index
CREATE CUSTOM INDEX inbound_idx ON inventory.inbound (p_index) USING 'com.stratio.cassandra.lucene.Index' WITH OPTIONS = {'schema': '{
fields : {
symbol : {type : "string",case_sensitive: false},
destination : {type : "string",case_sensitive: false},
ticket_id : {type : "string",case_sensitive: false}
}
}', 'refresh_seconds': '1', 'directory_path': '/c05/stratio_index/inventory/inbound'};
CREATE CUSTOM INDEX inbound_idx_new ON inventory.inbound (p_index_new) USING 'com.stratio.cassandra.lucene.Index' WITH OPTIONS = {'schema': '{
fields : {
symbol : {type : "string",case_sensitive: false},
destination : {type : "string",case_sensitive: false},
ticket_id : {type : "string",case_sensitive: false}
}
}', 'refresh_seconds': '1', 'directory_path': '/c05/stratio_index/inventory/inbound'};
Error:
inventory_admin#cqlsh:inventory> drop index inbound_idx_new;
ServerError: java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.NullPointerException
inventory_admin#cqlsh:inventory> drop INDEX inbound_idx;
ServerError: java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.AssertionError: attempted to delete non-existing file inbound
inventory_admin#cqlsh:inventory> drop index inbound_idx1;
inventory_admin#cqlsh:inventory>

Mongoose bulkWrite - Wrong type for 'q'

For my test suite, I want to bulkWrite test info in the database, and then bulk delete any of the test info entered throughout the test to come back to a clean slate. I do so by running a bulkWrite on the db to which I pass the content of a JSON file loaded via nodeJS's require statement.
The problem is that for the dataset
[ { deleteOne: { username: 'test-author' } } ]
passed to models[key].collection.bulkWrite(action[key]), where key is the name of the model of interest and action is the JSON file,I get the following error:
{ MongoError: Wrong type for 'q'. Expected a object, got a null.
at Function.MongoError.create (/var/www/website/server/node_modules/mongodb-core/lib/error.js:31:11)
at /var/www/website/server/node_modules/mongodb-core/lib/connection/pool.js:483:72
at authenticateStragglers (/var/www/website/server/node_modules/mongodb-core/lib/connection/pool.js:429:16)
at Connection.messageHandler (/var/www/website/server/node_modules/mongodb-core/lib/connection/pool.js:463:5)
at Socket.<anonymous> (/var/www/website/server/node_modules/mongodb-core/lib/connection/connection.js:339:20)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:548:20)
name: 'MongoError',
message: 'Wrong type for \'q\'. Expected a object, got a null.',
ok: 0,
errmsg: 'Wrong type for \'q\'. Expected a object, got a null.',
code: 14,
codeName: 'TypeMismatch' }
I have done some research and have been unable to find a solution to this problem. The error itself is pretty meaningless, so I can't grasp much out of it. Any idea how to solve the problem?
Any help would be greatly appreciated!
Cheers!
As per the MongoDB API, the deleteOne, deleteMany, updateOne, updateMany, replaceOne, and replaceMany operation requires to have a property filter which acts as the filter for the query.
However Mongoose's API shows the following (mistaken) example:
Character.bulkWrite([
...
{
deleteOne: {
{ name: 'Eddard Stark' }
}
}
]).then(handleResult);
Hence, the data sent over changes from:
[{
"deleteOne": { "username": "test-author" }
}]
to
[{
"deleteOne": { "filter": { "username": "test-author" }}
}]
I'll make sure to pass the message along to the mongoosejs dev group.

Sails-mongo many to many association not working

I am using sails 0.10.5 , and i have two models as below for many to many relation. but i am getting error while lifting app.
model 1) Category.js
attributes: {
name: {
type: 'string',
required: true
},
features: {
type: 'array'
},
desc: {
type: 'string'
},
sub_cats: {
collection: 'SubCategory',
via: 'cats',
dominant: true
}
}
model 2) SubCategory.js
attributes: {
cats: {
collection: 'Category',
via: 'sub_cats'
},
name: {
type: 'string',
required: true
},
features: {
type: 'array'
},
desc: {
type: 'string'
}
}
Now while lifting sails app, this error always trigger
"C:\Program Files (x86)\JetBrains\PhpStorm 8.0.2\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" app.js
c:\wamp\www\holymantra\hm\node_modules\sails\node_modules\waterline\node_modules\waterline-schema\lib\waterline-schema\utils.js:47
return hop.call(obj, prop);
^
TypeError: Cannot convert null to object
at hasOwnProperty (native)
at exports.object.hasOwnProperty (c:\wamp\www\holymantra\hm\node_modules\sails\node_modules\waterline\node_modules\waterline-schema\lib\waterline-schema\utils.js:47:14)
at JoinTables.parseAttribute (c:\wamp\www\holymantra\hm\node_modules\sails\node_modules\waterline\node_modules\waterline-schema\lib\waterline-schema\joinTables.js:148:26)
at c:\wamp\www\holymantra\hm\node_modules\sails\node_modules\waterline\node_modules\waterline-schema\lib\waterline-schema\joinTables.js:83:22
at Array.forEach (native)
So i am confused (my code is wrong or there is some problem in sails-mongo many-many relation)
--ND
It is just a typo in SubCategory.js: It should be attributes instead of attribute.
Hopefully you've solved your problem by now! For others like me who ran into this error, balderdashy/waterline-schema just released a more helpful error message at https://github.com/balderdashy/waterline-schema/issues/17 which should help you figure out your particular problem. Because as of today (2015-04-28) npm install does not get you this most recent version, you can manually fix it by doing the following:
Navigate to node_modules/sails/node_modules/waterline/node_modules/waterline-schema/lib/waterline-schema (note: this may be in your global sails directory)
Replace joinTables.js with https://github.com/balderdashy/waterline-schema/blob/8f5a44d076ca36edea409bf41ac2cdbf760c2c59/lib/waterline-schema/joinTables.js
I know this doesn't exactly answer your question as to what went wrong, but hopefully it will give others with that error the tools to solve it more quickly.

Error: Cyclic dependency: "User" with loopback passport example

I'm trying to work with Strongloop loopback and loopback-component-passport but can't quite figure out how to get the relationships between the built in User model, and the loopback-component-passport provided models, to work
It looks like the relationships are provided in the loopback-component-passport/lib/models/*.json files but I get the following error:
Error: Cyclic dependency: "User"
at visit (/work/node/loopback/myapp/node_modules/loopback-boot/node_modules/toposort/index.js:29:13)
at visit (/work/node/loopback/myapp/node_modules/loopback-boot/node_modules/toposort/index.js:43:9)
at toposort (/work/node/loopback/myapp/node_modules/loopback-boot/node_modules/toposort/index.js:22:22)
at module.exports.exports (/work/node/loopback/myapp/node_modules/loopback-boot/node_modules/toposort/index.js:10:10)
at sortByInheritance (/work/node/loopback/myapp/node_modules/loopback-boot/lib/compiler.js:248:21)
at buildAllModelInstructions (/work/node/loopback/myapp/node_modules/loopback-boot/lib/compiler.js:205:10)
at compile (/work/node/loopback/myapp/node_modules/loopback-boot/lib/compiler.js:79:27)
at bootLoopBackApp (/work/node/loopback/myapp/node_modules/loopback-boot/index.js:128:22)
at Object.<anonymous> (/work/node/loopback/myapp/server/server.js:44:1)
at Module._compile (module.js:456:26)
[stu:/work/node/loopback/myapp (master)]$
This is my model-config.json
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models",
"../node_modules/loopback-component-passport/lib/models"
]
},
"User": {
"dataSource": "db",
"public": true
},
"UserCredential": {
"dataSource": "db",
"public": true
},
"UserIdentity": {
"dataSource": "db",
"public": true
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false
},
"Role": {
"dataSource": "db",
"public": false
},
"KnowBit": {
"dataSource": "localpg",
"public": true
}
}
By enabling debug with loopback:boot:compiler
$ DEBUG=loopback:boot:compiler slc run
I'm able to get these logs:
[stu:/work/node/loopback/myapp (master)]$ DEBUG=loopback:boot:compiler slc run
INFO strong-agent API key not found, StrongOps dashboard reporting disabled.
Generate configuration with:
npm install -g strongloop
slc strongops
See http://docs.strongloop.com/strong-agent for more information.
supervisor running without clustering (unsupervised)
loopback:boot:compiler Found model "AccessToken" - ../node_modules/loopback/common/models/access-token.json ../node_modules/loopback/common/models/access-token.js +0ms
loopback:boot:compiler Found model "ACL" - ../node_modules/loopback/common/models/acl.json ../node_modules/loopback/common/models/acl.js +1ms
loopback:boot:compiler Found model "Application" - ../node_modules/loopback/common/models/application.json ../node_modules/loopback/common/models/application.js +0ms
loopback:boot:compiler Found model "Change" - ../node_modules/loopback/common/models/change.json ../node_modules/loopback/common/models/change.js +1ms
loopback:boot:compiler Found model "Checkpoint" - ../node_modules/loopback/common/models/checkpoint.json ../node_modules/loopback/common/models/checkpoint.js +0ms
loopback:boot:compiler Found model "Email" - ../node_modules/loopback/common/models/email.json ../node_modules/loopback/common/models/email.js +0ms
loopback:boot:compiler Found model "RoleMapping" - ../node_modules/loopback/common/models/role-mapping.json ../node_modules/loopback/common/models/role-mapping.js +0ms
loopback:boot:compiler Found model "Role" - ../node_modules/loopback/common/models/role.json ../node_modules/loopback/common/models/role.js +1ms
loopback:boot:compiler Found model "Scope" - ../node_modules/loopback/common/models/scope.json ../node_modules/loopback/common/models/scope.js +0ms
loopback:boot:compiler Found model "User" - ../node_modules/loopback/common/models/user.json ../node_modules/loopback/common/models/user.js +0ms
loopback:boot:compiler Skipping unknown module source dir "loopback/server/models" +0ms
loopback:boot:compiler Found model "KnowBit" - ../common/models/know-bit.json ../common/models/know-bit.js +1ms
loopback:boot:compiler Model source code not found: undefined - TypeError: Arguments to path.join must be strings +0ms
loopback:boot:compiler Found model "User" - ../common/models/user.json (no source file) +0ms
loopback:boot:compiler Skipping unknown module source dir "./models" +0ms
loopback:boot:compiler Found model "ApplicationCredential" - ../node_modules/loopback-component-passport/lib/models/application-credential.json ../node_modules/loopback-component-passport/lib/models/application-credential.js +1ms
loopback:boot:compiler Found model "UserCredential" - ../node_modules/loopback-component-passport/lib/models/user-credential.json ../node_modules/loopback-component-passport/lib/models/user-credential.js +0ms
loopback:boot:compiler Found model "UserIdentity" - ../node_modules/loopback-component-passport/lib/models/user-identity.json ../node_modules/loopback-component-passport/lib/models/user-identity.js +0ms
loopback:boot:compiler Using model "User"
Configuration: {"dataSource":"db","public":true}
Definition {"name":"User","plural":"users","base":"User","relations":{"accessTokens":{"type":"hasMany","model":"AccessToken","foreignKey":"userId"},"identities":{"type":"hasMany","model":"UserIdentity","foreignKey":"userId"},"credentials":{"type":"hasMany","model":"UserCredential","foreignKey":"userId"}},"validations":[],"acls":[],"methods":[]} +0ms
loopback:boot:compiler Using model "UserCredential"
Configuration: {"dataSource":"db","public":true}
Definition {"name":"UserCredential","base":"PersistedModel","properties":{"provider":{"type":"String","comments":"facebook, google, twitter, linkedin"},"authScheme":{"type":"String","comments":"oAuth, oAuth 2.0, OpenID, OpenID Connect"},"externalId":{"type":"String","comments":"The provider specific id"},"profile":{"type":"Object"},"credentials":{"type":"Object"},"created":"Date","modified":"Date","id":{"id":1,"generated":true}},"acls":[{"principalType":"ROLE","principalId":"$everyone","permission":"DENY"},{"principalType":"ROLE","principalId":"$owner","permission":"ALLOW"}],"relations":{"user":{"type":"belongsTo","model":"User","foreignKey":"userId"}}} +1ms
loopback:boot:compiler Using model "UserIdentity"
Configuration: {"dataSource":"db","public":true}
Definition {"name":"UserIdentity","plural":"UserIdentities","base":"PersistedModel","properties":{"provider":{"type":"String","comments":"facebook, google, twitter, linkedin"},"authScheme":{"type":"String","comments":"oAuth, oAuth 2.0, OpenID, OpenID Connect"},"externalId":{"type":"String","comments":"The provider specific id"},"profile":{"type":"Object"},"credentials":{"type":"Object"},"created":"Date","modified":"Date","id":{"id":1,"generated":true}},"acls":[{"principalType":"ROLE","principalId":"$everyone","permission":"DENY"},{"principalType":"ROLE","principalId":"$owner","permission":"ALLOW"}],"relations":{"user":{"type":"belongsTo","model":"User","foreignKey":"userId"}}} +0ms
loopback:boot:compiler Using model "AccessToken"
Configuration: {"dataSource":"db","public":false}
Definition {"name":"AccessToken","properties":{"id":{"type":"string","id":true},"ttl":{"type":"number","ttl":true,"default":1209600,"description":"time to live in seconds (2 weeks by default)"},"created":{"type":"Date"}},"relations":{"user":{"type":"belongsTo","model":"User","foreignKey":"userId"}},"acls":[{"principalType":"ROLE","principalId":"$everyone","permission":"DENY"},{"principalType":"ROLE","principalId":"$everyone","property":"create","permission":"ALLOW"}]} +0ms
loopback:boot:compiler Using model "ACL"
Configuration: {"dataSource":"db","public":false}
Definition {"name":"ACL","properties":{"model":{"type":"string","description":"The name of the model"},"property":{"type":"string","description":"The name of the property, method, scope, or relation"},"accessType":"string","permission":"string","principalType":"string","principalId":"string","id":{"id":1,"generated":true}}} +0ms
loopback:boot:compiler Using model "RoleMapping"
Configuration: {"dataSource":"db","public":false}
Definition {"name":"RoleMapping","description":"Map principals to roles","properties":{"id":{"type":"string","id":true,"generated":true},"principalType":{"type":"string","description":"The principal type, such as user, application, or role"},"principalId":"string"},"relations":{"role":{"type":"belongsTo","model":"Role","foreignKey":"roleId"}}} +0ms
loopback:boot:compiler Using model "Role"
Configuration: {"dataSource":"db","public":false}
Definition {"name":"Role","properties":{"id":{"type":"string","id":true,"generated":true},"name":{"type":"string","required":true},"description":"string","created":"date","modified":"date"},"relations":{"principals":{"type":"hasMany","model":"RoleMapping","foreignKey":"roleId"}}} +0ms
loopback:boot:compiler Using model "KnowBit"
Configuration: {"dataSource":"localpg","public":true}
Definition {"name":"KnowBit","base":"PersistedModel","idInjection":true,"properties":{"label":{"type":"string","required":true},"link":{"type":"string","required":true}},"validations":[],"relations":{},"acls":[],"methods":[]} +0ms
TL;DR
I had specified the same string "User" for the name and base attributes, to fix the problem I changed the name value to lowercase "user"
Full story
ok so when I copied the
loopback-example-passport/common/models/user.json
file I mistyped and set the name='User' instead of 'user'
This did not fix the ultimate problem, but I think it explains the Cyclic dependency. The 'base' is the 'base' class of this model and I had that set to 'User' as well
This allows me to get past the model compilation stage but still leaves me without a configured relationship from 'user' to 'UserIdentity' which the passport code requires.
I think I'm on the right track though, the insight being that the loopback models in the strongloop/loopback-example-passport example are Overriding the built in classes with new model classes where the first letter of the class name is lower cased for example userCredential is the override for the built in model class UserCredential and so on, this is a bit confusing at first
{
"name": "userCredential",
"plural": "userCredentials",
"base": "UserCredential",
...
But I think if I go through and check that I'm using the lower case versions things might work out
This was indeed the problem

Resources