How to get a better error message from Meteor in the case of an invalid DB update on a 'unique' indexed field? - mongodb

I've set a unique index on one of my Meteor.users fields:
Meteor.startup(function() {
Meteor.users._ensureIndex('profile.uri', {unique: 1, sparse: 1});
});
I'm allowing users to edit this field via a front-end form.
When they attempt to update it to something already taken by another user, the app - as it should - throws an error and prevents the update.
However, the error I'm getting is just a blank 500 Internal server error:
M…r.m…e.errorClass {error: 500, reason: "Internal server error",
details: undefined, message: "Internal server error [500]", errorType:
"Meteor.Error"}
Which… isn't hugely useful for client-side error reporting.
How can I get Meteor to return a more precise error message so that I can appropriately interpret the cause of the error and report it to the user?
Naturally if I apply an error message specific to that error ("URI already taken") to the 500 response then it'll show for any 500 error, which could end up causing confusion if something else is causing it.

There may be one temporary solution. Update through a method, and check in the body of that method if the specific field already exists in the collection.
I guess you could also do that in a update allowing hook.
Yes, it is doing again what is already done by the DB (except the DB is probably doing it better).
I can't see any other solution to have the right error message.
Meteor.methods({
updateProfileUri : function(newUri) {
if(!this.userId) {
throw new Meteor.Error('user not signed-in');
}
check(newUri, String);
//If another user already uses this URI
if( Meteor.users.findOne({ _id: { $ne: this.userId }, 'profile.uri' : uri }) ) {
throw new Meteor.Error('uri already taken');
}
updateUriOfUser(this.userId, newUri);
}
});

Related

How to Validate /check the input fields value exist in back end DB or not during live change in sapui5

I have requirement where in need to create the record from SAPui5 application,
For that we have Form and enterthe all details and submit to the data base.
Now i need to validate the first field value, if that value exist in the system/DB need to populate the error, like this record already exist during livechange.
For E.g., Input fields are as follows.
Empld : 121
EmpName : tom
On Change of Empid value need to check 121 record exist in the database or not.
Following are the blogs refereed for the solution but didn't get the solution for the same.
https://blogs.sap.com/2015/10/19/how-to-sapui5-user-input-validations/
https://blogs.sap.com/2015/11/01/generic-sapui5-form-validator/
As i"m new to SAPUI5.Please help me with the coding.
Thanks in advance.
I don't know how much you are aware of Requests to the Backend but maybe you could make a Read Operation and check if there is any data returned:
First solution could be like this (with Entity key):
this.getOwnerComponent().getModel().read("/EntityPath", {
success: function(oData, response) {
if(oData.results.length === 0) {
console.log("Nothing found for this key");
}
},
error: function(oError) {
//Error Handling here
}
});
Or you could build a Filter, pass it to the read operation and check if there is any data returned:
var aFilter = new sap.m.Filter(new Filter("EmpId", sap.m.FilterOperator.EQ, "value"));
this.getOwnerComponent().getModel().read("/EntitySet", {
filters: aFilter,
success: function(oData, response) {
if(oData.results.length === 0) {
console.log("User is not available");
}
},
error: function(oError) {
//Error Handling here
}
});
However, this isn't the best way to check if there is already an entry in your database. You should do this in your Business Logic with Error Messages which get passed to the Frontend.
Hope this helps :-)

Cannot read property 'length' of undefined on one GET request

working with a MEAN Stack and I have three GET requests for the same URL/Route. One is to get a generalised summary of long-term emotions, the other is to get a summary of emotions by dates entered, and lastly, a summary of emotions related to a user-entered tag associated with individual emotion entries.
My first GET request is throwing no issues but the second GET request throws an error: Cannot read property 'length' of undefined
The error points to the following line:
48| each emotion in dateEmotions
Below is the relative code associated with the error:
Jade
each emotion in dateEmotions
.side-emotions-group
.side-emotions-label
p.emotion-left= emotion.emotionName
p.pull-right(class= emotion.emotionLevel) (#{emotion.emotionLevel}%)
.side-emotions-emotion.emotion-left
GET Request
module.exports.emotionsListByDates = function (req, res) {
Emo.aggregate([
{ $match :
{ "date" : { $gte: ISODate("2018-04-09T00:00:00.000Z"), $lt: ISODate("2018-04-13T00:00:00.000Z") } }
}, { "$group": {
"_id": null,
"averageHappiness": {"$avg": "$happiness"},
"averageSadness": {"$avg": "$sadness"},
"averageAnger": {"$avg": "$anger"},
"averageSurprise": {"$avg": "$surprise"},
"averageContempt": {"$avg": "$contempt"},
"averageDisgust": {"$avg": "$disgust"},
"averageFear": {"$avg": "$fear"},
}}
], function (e, docs) {
if (e) {
res.send(e);
} else {
res.render('dashboard', {
title: "ReacTrack - User Dashboard",
pageHeader: {
title: "User Dashboard",
strapline: "View your emotional data here."
},
dateEmotions: docs
})
}
});
};
This question is already getting pretty long, but I have another GET Request pointed to that URL and it is not throwing any errors, and the only difference is that I am not matching the db records by date in that query. I can post the working code if need be.
Edit
After some experimenting, I am able to get each of the three routes working individually if I comment out the other two. It's when multiple routes pull in the multiple requests that causes issues. For example, here are the routes at present where the ctrlDashboard.emotionsListByDates is working:
// Dashboard Routes
//router.get(/dashboard', ctrlDashboard.emotionsListGeneralised);
router.get('/dashboard', ctrlDashboard.emotionsListByDates);
//router.get('/dashboard', ctrlDashboard.emotionsListByTag);
If I comment out two routes and leave one running, and comment out the respective each emotion in emotions each emotion in dateEmotions and each emotion in tagEmotions blocks in the Jade file and leave the correct one uncommented, then that route will work, it seems to be when I am firing multiple routes. Is this bad practice, or incorrect? Should all queries be in the one GET request if on the same URL?
Thanks.
Apologies, new to routing and RESTful APIs but after some researching into the topic, I now understand the fault.
I assumed that the URL used in routing was the URL you wanted the data to populate...which it still kinda is, but I thought if I wanted to populate the dashboard page, I had to use that exact route and I did not realise I could post the data to different URL routes and take the data from those URLs to populate the one page.
Fixed by adding /date and /tag to those routes and using AJAX to perform those requests and populate the main page.
Thanks all.
I have the same problem but I'm using React+Redux+Fetch. So is it not a good practice dispatch more the one request in the same time and from the same page to a specific url?
I would know what causes that problem. I've found some discussions about it could be a mongoose issue.
My code:
MymongooObject.find(query_specifiers, function(err, data) {
for (let i = 0; i < data.length; ++i) {
...
}
}
Error:
TypeError: Cannot read property 'length' of undefined

Meteor (Iron Router subscriptions): handling server errors with waitOn

I'm attempting to redirect my users to a 404 page (or even to home) if they stumble onto a page for which there is no database match.
My waitOn code (inside the Iron Router Route)
waitOn: function(){
return Meteor.subscribe('cars', this.params.slug);
},
My publish method:
Meteor.publish("cars", function (slug) {
var selectedCar = Cars.findOne({slug: slug})._id;
return [
Cars.find({ _id: selectedCar}),
Parts.find({carid: selectedCar}),
]
});
Everything is fine, except waitOn hangs whenever someone visits a page for which there is no matching Car (i.e the :slug doesn't match anything in the database)
Example Server error:
Exception from sub cars id CTusRZSAPqJaK9ws3 TypeError: Cannot read
property '_id' of undefined
I've tried all sorts of things as recommended on various blogs/posts, and still not sure how to deal with such server errors when waitOn is involved.
Has anyone been able to deal with such errors in their subscriptions?
In your current code you are not handling the case where findOne method might return undefined as a result.
Please modify your publication:
Meteor.publish("cars", function (slug) {
var selectedCar = Cars.findOne({slug: slug});
if (selectedCar) {
return [
Cars.find({ _id: selectedCar._id}),
Parts.find({carid: selectedCar._id}),
]
}
this.ready()
});
in above code if findOne returns an undefined result, we are calling the this.ready() method which will set the subscription ready.
on the client side if you don't receive any data in your subscription you can display the 404 message (item not found).
Also you should validate the slug, before querying. Just to avoid any nosql injection. for that you could use the check package.

Mongoose No matching document found using id() method. Error caused by asynchronous delete requests

Making asynchronous requests in a loop to delete documents from an embedded collection:
_.each deletedItem, (item) ->
item.$delete()
Erratically throws this error:
{ message: 'No matching document found.', name: 'VersionError' }
When executing:
var resume = account.resumes.id(id);
resume.remove();
account.save(function (err, acct) {
console.log(err);
if(err) return next(err);
res.send(resume);
});
After logging account.resumes and looking through the _id's of all of the resumes, the document I am attempting to find by id, exists in the collection.
530e57a7503d421eb8daca65
FIND:
{ title: 'gggff', _id: 530e57a7503d421eb8daca65 }
IN:
[{ title: 'asddas', _id: 530e57a7503d421eb8daca61 }
{ title: 'gggff', _id: 530e57a7503d421eb8daca65 }
{ title: 'ewrs', _id: 530e57a7503d421eb8daca64 }]
I assume this has to do with the fact that I am performing these requests asynchronously, or that there is a versioning issue, but I have no idea how to resolve it.
It doesn't make any sense to me how when I log the resumes, I can see the resume I attempt to find, yet if I log:
log(account.resumes.id(id));
I get undefined.
UPDATE
I've discovered that my issue is with versioning.
http://aaronheckmann.blogspot.com/2012/06/mongoose-v3-part-1-versioning.html
But I am still unsure how to resolve it without disabling versioning, which I don't want to do.
In mongodb version 3, documents now have an increment() method which manually forces incrementation of the document version. This is also used internally whenever an operation on an array potentially alters array element position. These operations are:
$pull $pullAll $pop $set of an entire array
changing the version key
The version key is customizable by passing the versionKey option to the Schema constructor:
new Schema({ .. }, { versionKey: 'myVersionKey' });
Or by setting the option directly:
schema.set('versionKey', 'myVersionKey');
disabling
If you don’t want to use versioning in your schema you can disable it by passing false for the versionKey option.
schema.set('versionKey', false);
MongooseJs API docs explicitly warn on disabling versioning, and recommend against it. Your issue is due to workflow. If you're updating your collection from the UI, sending the API request and not refreshing your object with the object from the backend -- then attempt to update it again, you'll encounter the error you are reporting. I suggest either consuming/updating the object scope from the API response, then __v is correctly incremented. Or don't send the __v field on the PUT API request, this way it won't conflict with version on the collection in the database.
Another option is -- when requesting the object from the backend, have the API response not send the __v field, this way you don't have to code logic to NOT send it from the frontend. On all your gets for that collection, do either one of the following (depends how you write your queries):
var model = require('model');
var qry = model.find();
qry.select('-__v');
qry.exec(function(err,results){
if(err) res.status(500).send(err);
if(results) res.status(200).json(results);
});
OR
var model = require('model');
model.find({}, '-__v', function(err,results){
if(err) res.status(500).send(err);
if(results) res.status(200).json(results);
});

Meteor error not being sent to client

I am trying to send a Meteor.Error to the client in the case where I have a duplicate key.
On the client, the 'error' in the callback is undefined (because at the moment, minimongo can't check for unique key indexes)
On the server the 'error' in the callback correctly throws an exception that a duplicate key exists and so it won't be inserted. However, the 'Meteor.Error' is never sent to the client.
Links.insert({
link: link_to_add,
user_id: this.userId
}, function(error, result) {
if (error != null) {
throw new Meteor.Error(409, 'Link already added');
}
});
What am I doing wrong? I am open to alternatives if there is a better way to do this.
It seems obvious to me now, but the problem was I wasn't capturing the error in the Meteor.call.
Meteor.call('add_link', {
link: link_to_add,
tag: tag_to_add
}, function(error, result) {
// the error is correctly being captured here
return console.log(error);
});