Determine Mongoose findByIdAndUpdate() error type - node.js

Re. mongoose findByIdAndUpdate(): How to determine proper error status code that whether id was not found (404) or passed data was invalid (422)?
Like alternatively we can use model.findById() first to determine 404 and then can use model.update() to determine 422.

Related

Key not being recognized in JSON

I have following JSON that i am trying to parse.
{"id":1,"colour":"blue","count(colour)":1}
It is a result of what i have returned from my sqlite3 select staement. I am doing a count(colours), which is being returned as the key in the JSON.
Then when i attempt to reference the value using .count(colour), my node app fails giving me an error that colour is not defined. Note, that referencing .id works just fine.
Has anybody ran into this issue before or can provide any help?
If your JSON is in some variable, myjson, and you access it directly with myjson.count(colours), you get the error because it's trying to execute a function in your object.
It works as you expect if you access via string like this: myjson["count(colour)"].

Customizing Validate Node Error Message in OSB 12c

When we add the Validate Node in the OSB 12c for validating the incoming request against XSD, and if the validation fails ,
in some fault messages the field name that is causing the validation error is displayed. But only for decimal values , fault message is just saying Invalid decimal Value and no mention about the field from where the error is thrown. Can we overcome this issue
I am not sure this is direct solution. But there is a workaround which may suit your need
Create an XQuery which validates the payload and throws custom error messages
eg: for xml element which should contain decimal value abc
if ($a instance of xs:long)
then ()
else (fn:error(xs:QName('Your error code'), 'your error message'))
This is a suitable method if the payload is small.
https://gibaholms.wordpress.com/2013/09/24/osb-throw-exception-in-xquery1
If the payload is large
identify the fields which are supposed to have these type of issues.
Create an XQuery for validating these fields with error messages.
Use validate node inside a stage and use a stage error handler
Validate the payload using xquery inside stage error handler

Loopback 3 discards error information on multiple validation errors, turning 422 to 500, how can I solve that?

I'm migrating from Loopback 2 tot 3.
I currently have an issue with validation errors and strong-error-handler
When I post a bulk create which results in multiple validation errors, those get returned as an array of ValidationErrors.
Those errors get grouped by strong-error handler in a 500 internal server error, which is how it was before, but the details of the errors get discarded, when debug is set to false.
In my example I upload an array of tags, but for each tag, a uniqueness validation is executed. When 2 or more tags are already in the database, I have an array of errors, instead of a single validation error
I need a way to determine why the validation failed on the client side, but the details of the errors are discarded now.
Am I doing something wrong here, or should this be considered as a bug?
From the strongloop error handler documentation in loopback,
In production mode, strong-error-handler omits details from error responses to prevent leaking sensitive information:
More information
For 5xx errors, the output contains only the status code and the status name from the HTTP specification.
For 4xx errors, the output contains the full error message (error.message) and the contents of the details property (error.details) that ValidationError typically uses to provide machine-readable details about validation problems. It also includes error.code to allow a machine-readable error code to be passed through which could be used, for example, for translation.
Am I doing something wrong here, or should this be considered as a bug?
No this is the intended behaviour
Safe error fields
You can set the stack trace as "safe-error-field" so that it will be displayed in production.
For example, the stack field is not displayed by default if you run the loopback in production mode.
If you still want to display the stack field, then change the config json in the server/middleware.json
"final:after": {
"strong-error-handler": {
"params": {
"safeFields": ["stack"]
}
}
}

MongoDB unique index custom error message E11000

Is there a way to set custom error message for 'E11000 duplicate key error' in MongoDB?
(Preferably, using Mongoose):
userSchema.index({ name: 1, email: 1 }, { unique: true });
1) You can use mongoose-unique-validator.
https://www.npmjs.com/package/mongoose-unique-validator.
This makes error handling much easier, since you will get a Mongoose validation error when you attempt to violate a unique constraint, rather than an E11000 error from MongoDB.
2)
Referenced in What am I doing wrong in this Mongoose unique pre-save validation?
you can also use pre save method in express
Schema.pre("save",function(next, done) {
//Here you can search if the record already exists and return custom message.
next();
});
You can easily customize and display error messages for unique: true validation errors using mongoose-beautiful-unique-validation.
To do so simply use the package mongoose-beautiful-unique-validation:
npm install --save mongoose-beautiful-unique-validation
Then you can simply use it as a global plugin (as below) or per schema.
const beautifyUnique = require('mongoose-beautiful-unique-validation');
mongoose.plugin(beautifyUnique);
For full insight and reference, please see this comment and the Readme on GitHub.
You may also want to use the package mongoose-validation-error-transform for displaying Mongoose validation error message(s).
No, not without changing the MongoDB source code and recompiling it with the new error message. You can swap out the message for one more to your liking with your application code. You could, for example, just wrap the index build call in a function that will return a different error message if a unique key constraint violation error occurs.

Remove collection from model RacerJS?

This is my client Side code:
model.remove('agent',{'text':'online'});
I cant able to remove collection from model,Its shows following error in console
Error: remove must be performed under a collection and document id. Invalid path: agent
From the documentation, it looks like 'path' should be the collectionname.id. In this case, maybe I suppose it should be 'agent.id'.
This blog refers that the path should be in the format collection.documentId.document.
http://blog.derbyjs.com/2012/04/13/derby-v0-dot-3-0/

Resources