discord.js username not defined - javascript

for reasons, I am trying to get the username using the user's ID, and so, I have tried many things but they all result in Cannot read property 'username' of undefined this is what I have:
user = bot.users.cache.find(user => user.id === key); //key is the user's id
console.log(user);
bot.channels.cache.get("847470926748057671").send(user.username); //this is where the error is
and where it outputs user to the log, it says
User {
id: '691615336943845407',
system: false,
locale: null,
flags: UserFlags { bitfield: 128 },
username: 'orangenal name',
bot: false,
discriminator: '7363',
avatar: 'e99b5e3d3edcfa7e22dd76a7eb869c62',
lastMessageID: '847498366236885053',
lastMessageChannelID: '847470926748057671'
}
so there is a username, but I do not know how to access it

Try using fetch() and make sure to handle the promise
bot.users.fetch(key)
.then(res => user = res)
.catch(console.error);
console.assert(!user, 'User was not found!');
const channel = bot.channels.cache.get("847470926748057671");
channel.send(user?.username);

Related

Why find() using models won't work in signup route?

It's a simple signup route to store credentials in a mongoDB database but I miss something because the 2 else if won't work properly. I suspect it is my find().
The first else if returns me in Postman "error": "E11000 duplicate key error collection: vinted.users index: email_1 dup key: { email: \"jean#dpont.com\" }" and the second give me "email already exists".
Thanks in advance for your help
const express = require("express");
const router = express.Router();
const SHA256 = require("crypto-js/sha256");
const encBase64 = require("crypto-js/enc-base64");
const uid2 = require("uid2");
const User = require("../models/User");
router.post("/user/signup", async (req, res) => {
try {
const email = req.fields.email;
const username = req.fields.username;
const phone = req.fields.phone;
const password = req.fields.password;
const token = uid2(64);
const salt = uid2(16);
const hash = SHA256(password + salt).toString(encBase64);
const emailSearch = await User.find({ email: email });
if (!emailSearch || username !== null) {
const newUser = new User({
email: email,
account: {
username: username,
phone: phone,
},
password: password,
token: token,
hash: hash,
salt: salt,
});
await newUser.save();
res.status(200).json({
_id: newUser._id,
token: newUser.token,
account: newUser.account,
});
}
//problem under
else if (emailSearch) {
res.status(404).json({ message: "email already exists" });
} else if (username === null) {
res.status(404).json({ message: "please type a username" });
}
} catch (error) {
res.status(404).json({
error: error.message,
});
}
});
It looks like the issue is that if the username in the request body is not null, it's going to attempt to create a new User with that username regardless of whether a User exists with the same email - if (!emailSearch || username !== null).
It's generally best-practice to do as much input validation as you can before you start looking for records or creating new ones, as you will be able to avoid more Mongo errors and database actions if you can stop invalid actions before they're attempted. So in this case, check that the username is valid before looking for existing Users.
To solve this problem, I would move that last else-if to before you check whether a User exists with the same email. That way, once you determine whether the username is valid, then the only thing that you need to consider is existing Users before creating a new one. Something like this:
if (username === null) {
res.status(400).send({ message: "Error: Please provide a 'username'." });
}
const existingUserWithEmail = await User.find({ email: email });
if (!existingUserWithEmail) {
// Create the new User
} else {
res.status(400).send({ message: "Error: An account already exists with this email." });
}

Sequelize how to properly create a User Data with constraints?

This question does not seek answer as "best practice", but rather the more appropriate way compared to what I have below.
Basically I have a User model/table that has this associate with UserScore model/table.
User.belongsTo(models.UserScore, {
foreignKey: {
name: "userScoreId",
allowNull: false,
defaultValue: 1
},
as: "score",
targetKey: "id"
})
And then the UserScore model is defined as:
'use strict';
module.exports = (sequelize, DataTypes) => {
const UserScore = sequelize.define('UserScore', {
id: { type: DataTypes.INTEGER, allowNull: false, autoIncrement: true, unique: true, primaryKey: true },
score: DataTypes.INTEGER,
coins: DataTypes.INTEGER
}, {});
UserScore.associate = function (models) {
UserScore.hasMany(models.User, {
foreignKey: "userScoreId",
sourceKey: "id"
})
};
return UserScore;
};
The PROBLEM that I have is that I feel like I'm doing my Sequelize queries below incorrectly.
First I create UserScore assuming that I know the userId, using Facebook works because I can get id from Facebook. BUT NOT with signup with email and password! This is the first problem.
And after creating the UserScore, I then create a User data. This just feels so wrong. Although it works well with login with Facebook because like I said, I have the userId for creating UserScore, but what if I don't have it (e.g. using sign up with email and password)?
EDIT: The question is related to the error about constraints, because I tried making a User data without making a UserScore first. I can't it reproduce now because I've fixed it using UUId (https://www.npmjs.com/package/uuid-int).
AuthController.js
module.exports = {
// SIGN UP WITH EMAIL AND PASSWORD
signup: async (req, res, next) => {
const email = req.body.email
const password = req.body.password
if (!email || !password) {
return res.status(403).send({
message: "Error! Required parameters are: {email} and {password}."
})
}
// Check if there is a user with the same email
db.User.findOne({
where: { email: email }
})
.then(data => {
if (data) {
return res.status(409).send({
message: "Email is already in use."
})
}
// Create a new user...
const newUser = {
fbId: null,
email: email,
firstName: null,
lastName: null,
photoUrl: null
}
const newUserScore = {
score: 0,
userId: id, //// <---------- NO IDEA!!!
coins: 0
}
db.UserScore.create(newUserScore)
.then(userScoreData => {
db.User.create(newUser)
.then(data => {
console.log("Created new user! ✅")
// Generate the token
const token = signToken(newUser)
// Respond with token
res.status(200).json({ token })
})
.catch(err => {
console.log("Error creating a new user!!!." + err)
return res.status(409).send({
message: "An error has occured while creating a new user"
})
})
})
.catch(err => {
console.log("Error creating user score.")
done(err, null, err.message)
})
})
.catch(err => {
console.log(err)
res.status(500).send({
message: "An error has occured while retrieving data."
})
})
}
}
User Relations:
I tried making a User data without making a UserScore first.
The foreign key constraint you have defined between User and UserScore
will never allow you to add a UserScoreId in the table User that does not exist in the UserScore table, as this will violate
the SQL foreign key constraint. This is indeed a constraint !
From what I understand (correct me if I'm wrong),
you are modeling the fact that a user may have one or more score(s),
if so, it's the userId that should be in the UserScore table.
This way, if the user with the same email does not exist you can add the user first then add the user score.
So you have to change how you defined your sequelize associations
and set userId as a foreign key on the UserScore table.
Here's how you could define the associations
//will add userId to UserScore table
User.hasMany(UserScore, {
foreignKey: { name: "userId", allowNull: false, defaultValue: 1 },
as: "score",
targetKey: "id"
})
//same here, will add userId to UserScore table
UserScore.belongsTo(User, {
foreignKey: "userId",
sourceKey: "id"
})

obj.hasOwnProperty is not a function error in a Firebase database query

I am deploying a cloud function in Firebase that acts as a OAuth2 middleware, handling the authorization and getting the token. After getting the access token and user details I want to store the token and user details in the database and also update or create the user. The code is from this example. But I'm always getting the same error message from function logs:
TypeError: obj.hasOwnProperty is not a function
at each (/srv/node_modules/#firebase/database/dist/index.node.cjs.js:553:17)
at validateFirebaseData (/srv/node_modules/#firebase/database/dist/index.node.cjs.js:1511:9)
at /srv/node_modules/#firebase/database/dist/index.node.cjs.js:1528:13
at each (/srv/node_modules/#firebase/database/dist/index.node.cjs.js:554:13)
at validateFirebaseData (/srv/node_modules/#firebase/database/dist/index.node.cjs.js:1511:9)
at /srv/node_modules/#firebase/database/dist/index.node.cjs.js:1528:13
at each (/srv/node_modules/#firebase/database/dist/index.node.cjs.js:554:13)
at validateFirebaseData (/srv/node_modules/#firebase/database/dist/index.node.cjs.js:1511:9)
at /srv/node_modules/#firebase/database/dist/index.node.cjs.js:1528:13
at each (/srv/node_modules/#firebase/database/dist/index.node.cjs.js:554:13)
Below is the function for creating a user
async function createFirebaseAccount(uid, displayName, email, accessToken) {
const databaseTask = admin.database().ref(`/idpAccessToken/${uid}`).set(accessToken);
const userCreationTask = admin.auth().updateUser(uid, {
displayName: displayName,
email: email,
emailVerified: true,
}).catch((error) => {
if (error.code === 'auth/user-not-found') {
return admin.auth().createUser({
uid: uid,
displayName: displayName,
email: email,
emailVerified: true,
});
}
throw error;
});
// execution stops here, when resolving this tasks
await Promise.all([userCreationTask, databaseTask]);
const token = await admin.auth().createCustomToken(uid);
console.log('Created Custom token for UID "', uid, '" Token:', token);
return token;
}
The variables uid, displayName, email and token are a plain text string in all four cases. The token returned from this function is later returned to the calling client using jsonp. I don't understand where this error comes from.

I need help accessing info inside an object

Working on a Discord bot and I need the user ID, which in this case is 'xxx'. Not sure how to get it.
I've tried
n.mentions.users.User. -- n.mentions.users.User()
What I have on my app:
bot.on('message', msg => {
if (msg.content === 'myId'){
msg.reply().then(n => {
console.log(n.mentions.users);
});
}
})
What I get back:
Collection [Map] {
'1803209281398201380913' => User {
id: 'xxx',
username: 'zzz',
discriminator: '0000'
}
I expect 'xxx' but get undefined.
Use this:
message.mentions.users.first().id

REST API: PUT but no need all data in json

This is my controller to update put user data. The controller accepts up to 4 values. I would like to do so if I send only a name to this route, This will change only the name and the rest will remain unchanged. (it will not be empty). Is it possible to do this? Do I have to do it in redux-saga, i.e. if it is empty, give it up-to-date
// Update basic User's Data
exports.setUserdata = (req, res) => {
const id = req.params.userId;
User.update(
{
password: req.body.password,
name: req.body.name,
surname: req.body.surname,
email: req.body.email,
},
{ where: { id } },
)
.then(() => {
res.status(200).json({ success: true });
})
.catch(() => {
res.status(500).json({ error: 'Internal server error' });
});
};
Pass params you want to update and don't pass other keys.
If req.body contains only name key, you can just pick up those 4 keys from req.body.
const updateParams = _.pick(req.body, ['name', 'password', 'surname', 'email'])
User.update(updateParams, { where: { id } })
If req.body has other properties with value null or undefined, you can filter them after picking.
const updateParams = _.chain(req.body).pick(['name', 'password', 'surname', 'email']).filter().value()
User.update(updateParams, { where: { id } })
Of course it depends on the ORM you are using but I believe most ORM don't update attributes which are not passed at all.

Categories