exclude a property from sequelize create method - javascript

I have a table called
users
has these columns
id
email
password
when I create a new user using const user = await User.create({email,password})
I got the created user stored in user
the key here is I don't want to have the password property in the user constant above
how to exclude a property from sequelize or using javascript logic..

Related

How can I access a specific attribute of a specific document in a mongoDB collection?

To summarize, I am working with 2 collections - 'usercollection' and 'groupcollection' and I would like to associate users with groups. I don't want to have 2 copies of all the user documents so I have a unique ID attribute for each user that I want to use to associate specific users with specific groups. This is all running on a localhost webserver so I'm getting the input from an html page with a form in it where you enter 'username' and 'groupname'. I tried using the .distinct() function with query as 'username' and the target field/attribute as 'uid'.
// Set our internal DB variable
var db = req.db;
// Get our form values. These rely on the "name" attributes
var userName = req.body.username;
// Set query and options for searching usercollection
var query = {"username" : userName};
const fieldName = "uid";
// Set our collections
var users = db.get('usercollection');
// Get UID corresponding to username
var uidToAdd = users.distinct(fieldName, query);
This is what I attempted (with some other lines that aren't relevant taken out) but it just returned a null object so I'm at a bit of a loss. Also, I'm still a beginner with nodejs/javascript/mongoDB so the more informative the answer the better! When I do the same code in the mongo shell I can get the actual value of the 'uid' attribute so I really don't know what's going wrong
I am not sure I am following you. But if I understood correctly, if you want to make a relationship between 'usercollection' and 'groupcolletion', you can simply create those 2 collections and each user in 'usercollection' should have a field with 'groupid' as a reference. In this way, you can access 'groupcollection' easily.
Here is an example with using mongoose.
In User model
...
groupId: {
type: mongoose.Schema.Types.ObjectID
ref: "Group"
}
...
Later you can also use 'populate' to fetch 'Group' information.
...
let data = await User.findById(id).populate('groupId');
...

postgres: relation does not exist when the table clearly exists when calling from node.js

so I psql'd and created table users;
CREATE TABLE users (
id integer NOT NULL,
username text
);
I am able to grab rows by doing SELECT * FROM users;
However, when I use node.js with the library module pg to make calls I get the infamous relation does not exist.
const createQuery = {text: "INSERT INTO users(id, username) VALUES($1)",values: values}
const { rows } = await db.query(createQuery);
I wasn't running into this issue before a complete server migration.
The most likely option is that the table was created in a different schema than the default schema that you are using from node.js.
You can search the schema your table belongs to by querying table information_schema.tables:
select table_schema from information_schema.tables where table_name = 'users';
With this information at hand, you can now qualify your table name with the proper schema name in the query that you are running from node.js. Assuming that the schema name is myschema, you would go:
INSERT INTO myschema.users(id, username) VALUES($1)
By default when you don't specify any schema, postgres points out to public schema. Please use schema name/ database name as along with the table name. Make sure you have provided proper configurations of the database within your code. If the configurations are proper and even you no need to provided schema alias within the query.

firebase assign a key to a specific browser instance when created?

I'm experimenting with Firebase/javascript and am wondering how do I assign a key to a specific browser instance when created?
Upon entry, user is greeted with a user input field for their name and submit button
When the submit button is clicked the user information is sent to firebase and saved under a newly created key
I know how to do this and how to retrieve the data from firebase but,
how do i ensure the key that is created gets assigned to the specific user so that only the information in that key is sent back to the user or updated?
I am trying to do this without having a user signup or login. Is there a way to do this?
When the user clicks the submit button, you will send the name and the key to the database:
var ref = firebase.database().ref();
var reference = ref.child('users');
var post = reference.push();
post.set({
username:name
});
Then after that you will have this in the database:
users
pushkey
username: userx
another user adds his name and clicks submit:
user
pushkey <--- this key will be only to this user
username: userx
pushkey1 <--- will be only to usery
username:usery

How do I read a username from the firebase realtime database in javascript?

Here is the structure of my database, where the long string of characters is the user id.
How do I read the username of the user if I know the user id is set to the variable uid?
This is a pretty basic firebase operation and if you haven't already, you should read their documentation.
But, if you have the user ID stored in a variable named uid, eg:
const uid = "14hxjh...";
Then you can read that data by doing (assuming users is a top level node):
firebase.database().ref(`users/${uid}/username`).once("value").then(snap => {
const username = snap.val();
// Do things with username.
})

parse server add current user to relation in users class (javascript SDK)

i want to do something like who visit your profile function in my ionic application, simply this function take the current user parse object and insert it in a relation column present in users class called who visited
so what i would like to do first is to query the relation to see if the current user already there and if not insert it in the relation
tried the following code:
var currentUser = Parse.User.current(); //current user object
var user = this.navParams.get('user'); // parse user object passed through navparams
var relation = user.relation("whoVisited");
relation.add(currentUser)
user.save()
but it gives me POST error 400 Bad Request
After searching for 6 hours i found the following
User class is protected and no user can modify other user data thats why i was getting the error
I managed to do the same function using cloud code and the power of master key

Categories