Username not changing properly using MongoDB update function? - javascript

Below is a snippet of my code where I begin by searching the collection of notes to see if any of them contain the username that I am changing my current session's username to. If the username has not yet been used, it may be changed to that so I change the current session's username and then I update every note to be under this new username then display a changesuccess.jade file. However, when I run the code everything appears to run fine exept the username for each note doesn't change. I feel like it's due to the find() method on the 5th line. Any help would be greatly appreciated!
router.post('/changeusername',function(req, res) {
var newUsername = req.body.username;
var user = req.session.username;
var userFound = notesCollection.find( { owner: newUsername } )
var results = function(infoInput) {
res.render("changedUser.jade", {title: "Username Change",
info: infoInput});
}
var checkChange = function(err) {
if (err) {
results("Username change failed!");
} else {
results("Username changed!");
}
}
console.log(userFound);
if (!userFound.length) {
notesCollection.update({ owner: user },
{ owner: newUsername},
{ multi: true },
checkChange);
} else {
res.render("changedUser.jade", {title: "Username Change",
info: "Username change failed!"});
}
});

If i understand your problem correctly, you are trying to update a collection in mongodb and it is not getting updated.
So the problem is with the way you are calling mongoose#update.
Since you want to update 'owner', you should use mongodb#$set
Mongoose supports the same $set operator in the conditions too.
So just a little correction for your case:
var conditions = { owner: user }
, update = { $set: { owner: newUsername }}
, options = { multi: true };
notesCollection.update(conditions, update, options, callback);
function callback (err, numAffected) {
// numAffected is the number of updated documents
})
So try this now.

Related

Why is document.remove() not working in Mongoose, Node.js?

Problem: I try to delete a document from the database using document.remove() in the following codes, but it does not remove this document from database. I am confident remove() is called because the pre hook I set up for 'remove' is called.
// Delete - delete a player and update the team
router.delete("/:player_id", function(req, res) {
Player.findById(req.params.player_id, function(err, foundPlayer) {
if(err) {
console.log(err);
req.flash("error", "Player you want to delete is NOT FOUND!");
res.redirect("back");
return;
}
foundPlayer.remove(function(err, removedPlayer) {
if(!err) {
console.log(removedPlayer); // prints null
}
}); // <<<<<<<<<<<
res.redirect("back");
});
});
I then use model.findByIdAndRemove() in the following codes, and it worked.
// Delete - delete a player and update the team
router.delete("/:player_id", function(req, res) {
Player.findByIdAndRemove(req.params.player_id, function(err, foundPlayer) {
if(err) {
console.log(err);
req.flash("error", "Player you want to delete is NOT FOUND!");
res.redirect("back");
return;
}
res.redirect("back");
});
});
I have two Schemas:
var TeamSchema = new mongoose.Schema({
name: String,
players: [
{
type: mongoose.Schema.ObjectId,
ref: "Player"
}
],
});
var PlayerSchema = new mongoose.Schema({
name: String,
team: {
type: mongoose.Schema.Types.ObjectId,
ref: "Team"
}
});
Thank you so much in advance!
remove() has been deprecated,
try this
Player.deleteOne(req.params.player_id,function(err, removedPlayer) {
if(!err) {
console.log(removedPlayer); // prints null
}
}); // <<<<<<<<<<<
You have used the .remove() on the foundPlayer which was returned by the findByID . You should use the remove directly on the model from which you are trying to remove the document. For eg. The following would work -
Player.remove({_id:req.params.player_id},function(err, foundPlayer){
if(!err)
console.log(foundPlayer);
});
If the player with given _id has been found you will get something logged onto the console like this -
deleted
{ n: 1, ok: 1, deletedCount: 1 }
NOTE :
Trying to use remove() will probably give you an warning saying -
DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.
So, you must use deleteOne or deleteMany instead according to your requirement. So your code should be like this -
Player.deleteOne({_id:req.params.player_id},function(err, foundPlayer){
if(!err)
console.log(foundPlayer);
});
You can also choose to use the following if you want to use the foundPlayerdoucment itself to be used in callback -
findOneAndDelete() / findByIdAndDelete() : Finds a matching document, removes it, passing the found document (if any) to the callback. Executes immediately if callback is passed, else a Query object is returned.
Hope this helps !

Meteor getting double insert on mongo database

The following code is an event to gathering information regarding expenses and done tasks into a bills database. Everytime I run this process I'm getting 2 entries into the bills database. The first entry is always empty and the second one has the entries I want.
'click .ConfirmCloseCase': function (event) {
var caseID = Session.get('CurrentClosingCaseID');
var TasksToChange = Tasks.find({caseID:caseID,done:true,billed:false});
var ExpensesToChange = Expenses.find({caseID:caseID,billed:false});
// Create new Entry into bills Database
Bills.insert({"expensestotal":0,"taskstotaltime":0}, function(error, result) {
// Set all Tasks to billed
TasksToChange.forEach(function(task){
Tasks.update(task._id, {$set: {"billed": true} })
Meteor.call( 'BillsUpsert', result, {$push: {"tasks": task._id}} );
Meteor.call( 'BillsUpsert', result, {$inc: {"taskstotaltime": task.hours}} );
})
// Set all Expenses to billed
ExpensesToChange.forEach(function(expense){
Expenses.update(expense._id, {$set: {"billed": true} })
Meteor.call( 'BillsUpsert', result, {$push: {"expenses": expense._id}} );
Meteor.call( 'BillsUpsert', result, {$inc: {"expensestotal": expense.amount}} );
})
Router.go('/Bills');
})
},
The Meteor call is like so:
Meteor.methods({
BillsUpsert: function( id, doc ){
Bills.update( id, doc );
}
});
My guess it that I'm getting 2 entries into the database is because the code is running asynchronously. Is this a correct assumption?... and as a followup is there an "easy" fix? :)
I don't think the asynchronous calls are the root cause of your problem but you can significantly simplify this code by moving all your inserts and updates to the server:
'click .ConfirmCloseCase': function (event) {
Meteor.call('closeCase',Session.get('CurrentClosingCaseID'),function(err,result){
if ( err ){
// handle error
} else {
Router.go('/Bills');
}
});
}
Method:
Meteor.methods({
closeCase: function(caseId){
// you need to implement security checks on caseId here!
// Synchronously create new document in Bills collection
var billId = Bills.insert({ expensestotal: 0, taskstotaltime:0 });
// Set all Tasks to billed
var TasksToChange = Tasks.find({ caseID: caseID, done: true, billed: false });
TasksToChange.forEach(function(task){
Tasks.update(task._id, {$set: { billed: true} });
Bills.update(billId,{ $push: { tasks: task._id }, $inc: { taskstotaltime: task.hours }} );
)};
// Set all Expenses to billed
var ExpensesToChange = Expenses.find({ caseID: caseID, billed: false});
ExpensesToChange.forEach(function(expense){
Expenses.update(expense._id, {$set: { billed: true} });
Bills.update(billId,{ $push: { expenses: expense._id }, $inc: { expensestotal: expense.amount }} );
)};
}
After much trial and error I found the problem. The double entry into my database was not related to the code I posted and was due to a stupid mistake of my part of calling the function twice from two different places in my code.
Sorry for wasting people's time with this post.

Collection helper throws a not defined error

Im trying to set up a list of "rooms". The intended sequence:
Click name of the user on his/her profile page
Check for existing room. If yes, go to that room, if not set up new room.
Im using both dburles:collection-helpers and reywood:publish-composite.
Its throwing me this error.
TypeError: Cannot read property 'username' of undefined
at Document.Rooms.helpers.recName (rooms.js:18)
And line 18 is:
return Meteor.users.findOne({ _id: this.receiver }).username;
i.e. _id: this.receiver is undefined.
I also tried to add protective checks in the collection helpers but error remains. I.e. return user && user.username for example.
One thing I note is that, I noticed when I click on the user, it goes to the room linked to the user's id. However when I click back, it jumps to a blank room with a different id thats unrecognised.
The relevant codes:
Server publish
Meteor.publish("onlusers", function (){
return Meteor.users.find({});
});
Rooms.js collection helper
Rooms.helpers({
recName: function() {
return Meteor.users.findOne({ _id: this.receiver }).username;
}
});
User.js (for profile page events)
Template.usersShow.events({
'click .user': function() {
var receiver = this._id;
Session.set('chatId', this._id);
var res = Rooms.findOne({
$or: [
{ owner : this._id },
{ receiver : this._id }
]
});
if(res){
Router.go('roomDetail', { "_id" : res._id });
} else {
var newRoom = Rooms.insert({
owner : Meteor.userId(),
receiver : receiver,
username : Meteor.user().username,
});
Session.set('roomid', newRoom);
Router.go('roomDetail', { "_id" : newRoom });
}
}
});
Your diagnosis:
_id: this.receiver is undefined.
May be misleading. What is also possible is that the user subscription isn't completely loaded when your helper runs. I was helping someone else with a similar problem with publish-composite the other day - the subscription is marked as ready when the parents are ready but the children may not have finished loading yet. I think of this as a bug in publish-composite, all the related objects really need to be there before the subscription can be marked as ready.
Instead of returning:
return Meteor.users.findOne({ _id: this.receiver }).username;
You can do:
var user = Meteor.users.findOne({ _id: this.receiver });
return user && user.username;
So you'll get nothing back until the user object loads but you won't throw an error.

startAt and endAt not working as expected using angularfire : 0.8.0

Using AngularFire 8.0
Bellow is the code which uses startAt and endAt to limit the data from firebase.
$rootScope.$on('$firebaseSimpleLogin:login', function(e, authUser){
var queryRef = ref.startAt(authUser.uid).endAt(authUser.uid);
var queryArray = $firebase(queryRef).$asArray();
queryArray.$loaded().then(function() {
setCurrentUser(queryArray.$keyAt(0));
});
});
The data returned should be a single element from firebase but queryArray is empty when I use console.log for debugging.
Without the use of startAt and endAt, the queryArray contains all the elements stored in the firebase.Therefore, logging queryArray.$keyAt(0) gives the First elements name as output. Which is as expected.
I have checked the release notes of Firebase 8.0 as well and I don't see any changes in these limiters.
Please point out if any syntactical mistake or any alternative solution which can achive the desired result.
I basically want single record from the Firebase, which is my current user , authUser is the authorized user with authUser.uid as its priority.
Following is the JSON file which is populated in the Firebase when a user registration happens.
{
"users" : {
"User A" : {
"md5_hash" : "d10ca8d11301c2f4993ac2279ce4b930",
"setPriority" : "simplelogin:69",
"username" : "User A"
},
"User B" : {
"md5_hash" : "2076105f6efe7c11e285add95f514b9a",
"setPriority" : "simplelogin:70",
"username" : "User B"
},
"User C" : {
"md5_hash" : "a6d14de05d7b2c3cf4fae7ae14cfa7f3",
"setPriority" : "simplelogin:71",
"username" : "User C"
}
}
}
After Edit
Using the bellow code to get the priority:
queryRef.once('value', function(nameSnapshot) {
var val = nameSnapshot.getPriority();
console.log("Priority is: " + val );
});
Log output is:
Priority is: null
The method used to add user to the Firebase is:
create: function (authUser, username) {
users[username] = {
md5_hash: authUser.md5_hash,
username: username,
setPriority: authUser.uid
};
users.$update(username, {
md5_hash: authUser.md5_hash,
username: username,
setPriority: authUser.uid
//$priority: authUser.uid
}).then(function () {
setCurrentUser(username);
});
}, // end of create method
It looks like the priority on all of your data is null. This prevents endAt and startAt from working properly.
The clue that something is up is the existence of the setPriority key in your data. Priority is metadata that's managed outside the normal view.
Change your user creation code to something like this:
create: function (authUser, username) {
users[username] = {
md5_hash: authUser.md5_hash,
username: username,
.priority: authUser.uid
};
} // end of create method
or this:
create: function (authUser, username) {
users[username] = {
md5_hash: authUser.md5_hash,
username: username
};
users.$update(username, {
md5_hash: authUser.md5_hash,
username: username
}).then(function (dataRef) {
dataRef.setPriority(authUser.uid);
setCurrentUser(username);
});
} // end of create method

Sails.js Set model value with value from Callback

i need to provide something like an association in my Model. So I have a Model called Posts with an userid and want to get the username from this username and display it.
So my ForumPosts.js Model looks like the following:
module.exports = {
schema: true,
attributes: {
content: {
type: 'text',
required: true
},
forumTopicId: {
type: 'text',
required: true
},
userId: {
type: 'integer',
required: true
},
getUsername: function(){
User.findOne(this.userId, function foundUser(err, user) {
var username = user.username;
});
console.log(username);
return username;
}
}
};
I know that this return will not work because it is asynchronus... But how can i display it in my view? At the Moment i retrive the value with:
<%= forumPost.getUsername() %>
And for sure get an undefined return...
So the question is: How can I return this value - or is there a better solution than an instanced Model?
Thanks in advance!
Off the top of my head, you can just load associated user asynchronously before rendering:
loadUser: function(done){
var that = this;
User.findOne(this.userId, function foundUser(err, user) {
if ((err)||(!user))
return done(err);
that.user = user;
done(null);
});
}
then in your controller action:
module.exports = {
index: function(req, res) {
// Something yours…
forumPost.loadUser(function(err) {
if (err)
return res.send(err, 500);
return res.view({forumPost: forumPost});
});
}
}
and in your view:
<%= forumPost.user.username %>
This is kind of a quick and dirty way. For a more solid and long-term solution (which is still in development so far) you can check out the alpha of Sails v0.10.0 with the Associations API.
So this particularly case of associations between your models. So here you have a User model and ForumPost model and you need the user object in place of your user_id as user_id works as a relationship mapping field to your User model.
So if your are using sails V0.9.8 or below you need to handle this logic in your controller where ever you want to access User model attributes in your view.
In your controller write your logic as:
model.export = {
//your getForumPosts method
getForumPosts : function(req,res){
var filters = {};
forumPost.find(filters).done(function(err,posts){
if(err) return res.send(500,err);
// Considering only one post obj
posts = posts[0];
postByUser(posts.user_id,function(obj){
if(obj.status)
{
posts.user = obj.msg;
delete posts.user_id;
res.view({post:posts});
}
else
{
res.send(500,obj.msg);
}
});
}
}
}
function postByUser(user_id,cb){
User.findOne(user_id).done(function(err,user){
if(err) return cb({status:false, msg:err});
if(user){
cb({status:true, msg:user});
}
}
}
and then you can access your post object in your view.
Or else you can keep watch (at GitHub) on next version of sails as they have announced associations in V0.10 n it is in beta testing phase as if now.

Categories

Resources