I've got CouchDB setup with Couchperuser. Locally I use PouchDB.
I'm building a mobile application with Cordova. It's about a todo list. with login, so users have there own list.
When I create a new user it automatically makes a new database for this user.
now, when that user is logged in and adds new todo's to his list, they go in the main database because I use :
var db = new PouchDB('http://localhost:5984/main', {skipSetup: true});
What I want to achieve is that the todo created by the specific user goes into his database. for example:
var db = new PouchDB('http://localhost:5984/userdb-41646d696e32', {skipSetup: true});
How can I automatically do this? so the var db = the users database?
I've looked around the internet but could not find anything about this.
Hope someone can help me with this scenario.
EDIT:
I'll add the code that I use for adding the todo:
var db = new PouchDB('http://localhost:5984/main', {skipSetup: true});
function addToDoItem() {
//get info
var toDoTitle = document.getElementById('toDoTitle').value;
var toDoDescr = document.getElementById('toDoDesc').value;
var addItem = {
_id: new Date().toISOString(),
title: ToDotitle,
description: ToDoDescr
};
db.put(addItem ).then(function (result){
console.log("Added to the database");
console.log(result);
}).catch(function (err){
console.log("someting bad happened");
console.log(err);
});
}
Find out, this is the way to fix it. thought of it already but seemed a bit of a cheap solution. anyway, this is the only code example I found on the internet.
dbs.remote.private = pouchDB(DATABASE.URL + "userdb-" + _convertToHex(username), {
auth: {
username: username,
password: password
}
});
Related
I'm developing an app in Meteor and I want to know how I do can delete a user's account who is logged into the system? I mean that you can delete your account (like Tinder or Facebook) and the application ejects you because you're already deleted, and you no longer exist.
With a simple button of "Delete your account" attached.
If you could help me; I'm still a novice I'd really appreciate it, I try to retrieve the id of the current user with Meteor.userId(), and I was creating a method in the following way:
Meteor.methods({
SuprimirPersona: function(id) {
var postId = Meteor.userId();
const userId = this.userId;
const p = Meteor.users.findOne(postId);
if (userId && p === userId) {
Meteor.users.remove({
postId: this._id
},
function(error, result) {
if (error) {
console.log("Error removing user:", error);
} else {
console.log("users removed:" + result);
}
})
}
}
});
And calling the method in the following way but it does not give any results, I do not understand why:
'click #Desactivarr': function() {
var postId = Meteor.userId();
Meteor.call('SuprimirPersona', userId, function(error, result) {
if (error) {
console.log("Somee Error");
}
});
Meteor.logout(function() {
FlowRouter.go('/');
});
},
Hope someone could help me! Regards!
I've just answered this question on Meteor Forums:
https://forums.meteor.com/t/how-to-remove-logged-in-user-in-meteor/42639/3
The issue is you're trying to remove Users by postId, instead of Users.remove({_id: id}). You're removing nothing :)
To remove a user from the user's collection. You need to get the userid of the user you want to remove. This you can get by calling Meteor.userId() on the client to get the userid of the user or this.userId on the server. You need to logout the user and after a successful logout you can pass the userid you got to the meteor.users.remove(userId)
You are doing some unnecessary things on the client and server side - like getting the same user id multiple times, not even passing it to the server side method and then getting it again.
I think what you are trying to do is get the id of a user that posted something and pass it to the server side, where you check if the poster's id is the same as the id of the current user. If so, you remove the user, otherwise nothing happens.
'click #desactivarr' : function() {
var postID = <get the id of the user you want to delete here>;
Meteor.call("suprimirPersona", postID, callbackfunc);
}
Then on the server side it would be
Meteor.methods({
suprimirPersona : function(postID) {
var userID = Meteor.userId();
if (userID && postID === userID) {
Meteor.users.remove({ postId : userID })
}
}
});
Meteor.userId() and this.userId return the id of the currently logged in user that is executing the code on the client side or making the request to a server side method.
Hello I am new to NodejS and still learning so I might have mistakes and I am trying to learn it by writing code and I am from java background. Asyc functions are still new to me.
Here is my question
I would like to assign user_id to each user from my code.
.post('/register', function (req, res, next) {
var username = req.body.uname;
var password = req.body.psw;
var email = req.body.uemail;
var fullname = req.body.fullname;
var user_id = sql_counts.sql_count()+1;
console.log('Your user id is'+user_id);
sql_connection.User.create({
username:username,
password:password,
email:email,
user_id:user_id
},function (err) {
if(!err){
console.log('Successful');
res.redirect('/registrationComplete');
}else{
console.log('Error creating');
}
});
});
And here is my sql_count function:
function sql_count() {
sql_connection.User.count().then(function (err) {
initCounter =err;
});
return initCounter;
};
I would like to understand more about calls backs since this is very new and confusing to me. Could anyone suggest some materials,links,videos... Although it seems to be understanding in the start I just dont understand it when I try using it. And also how to return values from callbacks to outter function?
Say
In the code given above how do I return value from the 'then' part to outer function sql_count() ?
Use MYSQL AUTO_INCREMENT feature to create a new user_id for each new user (i.e each new row) while inserting as follows
CREATE TABLE USERS( user_id INT NOT NULL AUTO_INCREMENT);
cheers
For Callbacks and Promises checkout this video and for latest await /async feature this video
You can use auto increment column in you mysql table it will automatically increase or generate id using random function. I will suggest you first one
I want to add an array of current user object ID in to a user's column called "followers". Due to Parse's security reason not allowing modification to non-current user, I'm forced to use cloud code. The problem is I know nothing about JavaScript, so I need help here.
Here's what I would code if no security issue mentioned above:
//add the current user ID to the user(userPassed) that the current user liked
[userPassed addUniqueObject:[PFUser currentUser].objectId forKey:#"followers"];
[userPassed saveInBackground];
To be very specific, I just want to know how to code the above in cloud code. Thanks.
Here you go:
Parse.Cloud.define('functionName', function(request, response) {
var userId = request.params.userId;
var me = Parse.User.current();
var user = new Parse.User();
user.id = userId;
user.addUnique('followers', me);
return user.save(null, {useMasterKey: true}).then(function(user) {
response.success('Succeed');
}, function(error) {
console.error(error);
response.error('Failed');
});
});
Presently I am using Cloud Code along with the masterkey to modify attributes of other users based on actions of current user. I have an attribute requests_from, and I have managed to add items to this array based on the following code:
Parse.Cloud.define('requestUser', function(request, response) {
var userId = request.params.userId,
requestsFrom = request.params.requestsFrom;
var User = Parse.Object.extend('_User'),
user = new User({ objectId: userId });
user.add('requests_from', requestsFrom); //Changed from set to add
Parse.Cloud.useMasterKey();
user.save().then(function(user) {
response.success(user);
}, function(error) {
response.error(error)
});
});
I have used the above code to add currentUser.objectId to a target user's requests_from column.
However, my issue is now deleting items from requests_from. Once the current user has done an action, I would like to remove currentUser.objectId from requests_from. However, using user.remove or user.delete isn't working. I was wondering if there is another way to accomplish this via Cloud Code?
Thanks
I hope this answer helps you if you haven't already solved it ! Basically, after researching and failing to find anything, it appears that the solution to this is to do the following:
Parse.Cloud.define('removeRequest', function(request,response){
var userId = request.params.userId;
var User = Parse.Object.extend('_User'),
user = new User({ objectId: userId });
var currentUser = request.user;
var relation = user.relation("request");
relation.remove(currentUser);
Parse.Cloud.useMasterKey();
user.save().then(function(user) {
response.success(user);
}, function(error) {
response.error(error)
});
});
I want to update a field within the User class without being logged in as a user. From reading online and other responses people say I should use the 'masterkey' to do so. Here is my cloud code where I have added in the master key. The code is executed but when I go to my data browser the totalScore and predictions values are still the same and not updated to the new values.
Parse.initialize("key", "key");
Parse.Cloud.define("userUpdate", function(request, response) {
Parse.Cloud.useMasterKey();
var publicReadACL = new Parse.ACL();
publicReadACL.setPublicWriteAccess(true);
request.object.setACL(publicReadACL);
var User = Parse.Object.extend("User");
var query = new Parse.Query(User);
query.equalTo("username", request.params.username);
query.find({
success: function(user) {
user.set("totalScore", request.params.totalS);
user.set("totalPredictions", request.params.totalG);
user.save()
},
error: function() {
response.error("f");
}
});
});
Any help would be massively appreciated.
Hopefully you've figured this out by now, but if you haven't...I would first say check to make sure that you're passing the "totalScore" and "totalPredictions" as numbers. If you pass them as strings and Parse is expecting a Number, it won't update. And generally, I believe it's best practice to query the user class as follows:
var query = new Parse.Query(Parse.User);
query.get(user.objectId, {
success: function(userAgain) {
userAgain.set("totalScore", totalScore);
userAgain.save(null, {
error: function(userAgain, error) {
// This will error, since the Parse.User is not authenticated
}
});
}
});
Then of course you'd still need to include the master key stuff etc...