I am having an issue with Sonar Cloud and SonarQube, and I was hoping someone might be able to help me troubleshoot it.
Here is a brief description of the problem:
I am trying to create a new document in Mongoose, but SonarQube is giving me the error “Replace User with a constructor function.” I have already defined the User object as a constructor function using the mongoose.model() function, but the error persists.
Here is the relevant code snippet:
const testData = {
...userData,
profileImage: { picture }
};
const testSonarQube = new User(testData);
Here is some additional information that might be helpful in troubleshooting the issue:
Sonar Cloud Scan Version: sonarcloud-scan:1.4.0
Sonar Cloud Quality Gate Version:0.1.6
Programming language: js
Error message: “Replace User with a constructor function.”
I would really appreciate any help or advice that anyone might be able to offer. Thank you in advance for your assistance!
SonarQube Community member mentioned "Indeed mongoose.model is not a case the JS engine considers for that rule." and they created a ticket to implement it
https://github.com/SonarSource/SonarJS/issues/3646
Related
I am getting an error when I am trying to run:
(node:9164) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'applications' of undefined
Here is my code:
const discord = require('discord.js');
const client = new discord.Client();
const guildId = '820368493017825333';
client.on('ready', async () => {
console.log('ready');
const commands = await client.api.
applications(client.user.id)
.guilds(guildId)
.commands.get();
console.log(commands);
});
client.login(require(`./config.json`).Token);
Issues With the Accepted Answer
The accepted answer is incorrect in several ways. I'll walk through the inaccuracies in that answer and highlight the more likely causes of this problem, for the sake of anyone that may stumble upon this question in the future (and would've been misled by the accepted answer into believing that Slash Commands cannot be implemented in discord.js).
Well, the answer is pretty simple here. According to Discord.js docs, Class Client doesn't have api property. That's why you have the undefined error.
Incorrect. The Client class does have an api property. Or rather, it inherits the api property from the BaseClient class that it extends. This can be found in the source code of BaseClient. It is true that this is not documented in the discord.js docs. That is intentional, as the api property is intended to be a private property, more for discord.js' own use than for general use. You may notice in the source code that the property is annotated with #private, which usually indicates that it will not appear in the docs. There are many such private properties and methods that exist in discord.js classes, which are undocumented but are usable in your own code.
It seems like the tutorial that you are looking at is a bit outdated, or probably the tutor adds this property manually because Discord.js have relevant classes, like Application and ClientApplication but I still don't see an api property there as well.
The tutorial that the OP was going off of was actually more up-to-date than the tutorials posted and used by the accepted answer. The Application and ClientApplication classes are not at all relevant, as neither can access Slash Commands. Nor did hundreds of different tutorials each implement their own api property that all work in exactly the same way; they were all using the api property included in the latest versions of discord.js.
If you want to implement commands to your Discord bot with slash support, just add the following code, after ready stage.
The accepted answer misunderstood what 'Slash Commands' are, and provided code simply for creating a command with a slash for a prefix. That is not what the Slash Command system is. Slash Commands allow you to do things such as documenting, autocompleting, and validating commands and command arguments that users are typing in, in real-time while they are entering their input.
Not it shouldn't. Actually, the Discord.js lib is updated more often, the [YouTube] creators do it with their videos. I have already placed in my answer, a relevant guide made by the Discord.js community.
Yes it should. Hundreds of tutorials used the same code as each other, containing the api property, in instructing developers on how to work with Slash Commands in unmodified discord.js. I am not sure what exactly was meant by this comment.
If you look at the actual source code of discord.js, you'll find that the latest versions use the client's api property several times internally, usually in methods that directly query the Discord API for information (such as .fetch() methods). If the api property is undefined and you are using the latest version of discord.js, then much of your bot would not be working properly. So the latest client class not having an api property is not the main issue, which leads us to what the main issue really is.
So What Is the Real Issue?
There truly isn't enough context provided in the question to know for sure what exactly was causing the issue in the question. However, we can narrow the cause down to a few potential suspects, especially given the information aforementioned. Double check these to ensure they are not causing your problem:
Discord.js version. The api property does not exist for versions older than v12. Make sure you are using the latest version of discord.js. This is most likely the cause of the issue.
Missing access. You need to give your bot the application.commands scope when generating its invite link, otherwise you cannot interact with or create Slash Commands. This shouldn't really cause the api property to be undefined and should give you a different error, but it's worth double-checking.
If working with Slash Commands in simple discord.js is still not working for you even after double-checking both of these potential issues, you may want to consider an alternate (and somewhat simpler) approach to implementing Slash Commands: the discord-slash-commands-client module.
You would initialize this module like so:
const interactions = require("discord-slash-commands-client");
const iclient = new interactions.Client(
"you unique bot token",
"your bots user id"
);
Then to get a list of all existing Slash Commands, as the code in this question is attempting to do, all you would need to do with this module is:
let commands = await iclient.getCommands();
A single, clean line. As simple as it gets. The only downside to this alternate approach is that this module may not stay up-to-date as reliably as discord.js itself does. However, it would certainly be helpful if you are not able to figure out how to get Slash Commands working in discord.js itself.
If you need more help on this or want to see a more complete implementation of either approach, this question has several good, working examples on how to get Slash Commands code working properly on your bot.
This answer is a outdated!
When it was accepted Discord haven't introduced truly /slash commands. So use the answer below, if you want to integrate or migrate to newest version of Discord.js
Well, the answer is pretty simple here. According to Discord.js docs, Class Client doesn't have api property. That's why you have the undefined error.
It seems like the tutorial that you are looking at is a bit outdated, or probably the tutor adds this property manually because Discord.js have relevant classes, like Application and ClientApplication but I still don't see an api property there as well.
If you are looking for a good guide, I might recommend you this one from the official Discord recommendation page.
If you want to implement commands to your Discord bot with slash support, just add the following code, after ready stage.
const prefix = '/'
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'ping') {
message.channel.send('Pong.');
}
})
I'm brand new to coding, but decided to give it a shot just to make a fun little bot for a Discord server to surprise my friends, but I'm having a bit of an issue with refactoring in Visual Code Studio - for some reason, my forEach code isn't working all of a sudden, and I can't seem to figure out why or how to fix it,
here's the error I'm getting whenever I try to run it
files.forEach(file => {
^
TypeError: Cannot read property 'forEach' of undefined
and here's the section of code I'm using
fs.readdir("./events/", (err, files) => {
files.forEach(file => {
const eventHandler = require('./events/message.js')
const eventmessage = file.split(".")[0]
client.on(eventmessage, (...args) => eventHandler(client,...args))
})
Like I said, I'm new at coding, and have largely been following online guides, but I've tried a few things, such as removing it, restarting the program, rewriting it in a different window to see that does anything, reinstalling what I'm using in the code, and so on
I'm probably being an absolute idiot and missing some piece but I'm totally baffled so any guidance (or a different way to refactor!) would help!
Thank you in advance!
Note: I am largely following this guide if that helps any!
https://thomlom.dev/create-a-discord-bot-under-15-minutes/
EDIT: Here's a coded copy of what I've been following, as far as I can see my code is basically exactly the same
https://github.com/thomlom/discord-bot-example
Maybe you forgot to create the events folder, because I got the same error when the folder doesn't exist.
Try to follow this part of the tutorial again
To modularize our code, we will create an events folder. This folder will contain .js files whose name will match the different events name discord.js listens to:
Create an events folder.
In this folder, create three files: ready.js, message.js and guildMemberAdd.js.
Hoping someone can shed some light on using Cognito with plain JavaScript. No npm, no webpack, just plain JavaScript. I found a post that had a great example including the required AWS JavaScript libraries. I worked from this example until I ran into the problem of sign out not working. I thought perhaps the libraries in the example were out of date so I went looking for the latest. This is where things got confusing. In the example I had the following JavaScript libraries - amazon-cognito-identity.min.js, aws-cognito-sdk.min.js, and aws-sdk.min.js. I assume that aws-cognito-sdk must no longer exist? I updated the other two and see that there is an amazon-cognito-auth library. Do I need that? Anyhow with those three libraries the existing code no longer functions. I end up with errors like "AWSCognito is undefined" etc.
Hoping someone can point me in the right direction and show me where the downloads, documentation, etc for using Cognito in plain JavaScript are
Find hereunder a template I created for myself to work from. It is in "plain javascript" and in an angularjs app.
My template: https://github.com/PouncingPoodle/aws-cognito-angularjs/tree/master
based on: https://github.com/takanorig/aws-cognito-angularjs
Always make sure that you use the correct versions of scripts and the correlating apiVersion when you call a Lambda function for example
There might be bad practice involved but as far as I could research it should be all fine.
You can perform the logout using below code snippet. I haven't faced such strange errors while using this function.
var poolData = {
UserPoolId: '...', // Your user pool id here
ClientId: '...', // Your client id here
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
Username: 'username',
Pool: userPool,
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.signOut();
if you want to make the user logout from all the devices that he has got logged in from, then you can replace the last line of previous code with below code line.
cognitoUser.globalSignOut(callback);
You can refer below article to get a better understanding about the usage of amazon-cognito-identity-js package.
https://medium.com/#sankharathnayaka/aws-cognito-authentication-without-using-amplify-8d0e3af997c2
Solution Found: Turns out the problem was a "circular" dependency. For anyone who else might encounter this problem, I found the answer here: Require returns an empty object
First time asking a question. I'm still learning, and I thought I knew what I was doing here, but I can't make sense of this.
I am working on a Node project with MongoDB/Mongoose. At the top of my file, I have:
const {Institution} = require('./institution');
const {Student} = require('./student');
When I run my program and use Institution.findOne() it errors and says cannot read property findOne of undefined. I use the Institution model in several other files and they all work fine.
The model is required in several steps before this one and always works fine. Since the model works in other cases, I would think the exports are working just fine.
const Institution = database.model('Institution', InstitutionSchema);
module.exports = {
Institution,
InstitutionSchema
};
Both institution.js and student.js are in the same folder as this file.
When I do console.log(Student), it returns the huge Mongoose object.
When I do console.log(Institution), it returns undefined.
If I console.log(Institution) from another module where it is working, it returns the Mongoose object.
The Institution collection is in my database and I can view the content in Robomongo viewer.
Am I missing something that I just can't see?
Thanks in advance for any help.
I'm interested in customizing the Rally Iteration Health.
When I grabbed the code and assumed I would proceed by using the file "app-debug-tpl.html" to test it, unmodified. I'm getting javascript error:
"Expected an operand but found <"
on line 13:
Rally.loadScripts(<%= JSON.stringify(js_files) %>
I'm awfully confused about what's causing that error. Any help pointing me toward what I'm missing is appreciated. I've deployed the "\Deploy\app.txt" and it works, so I figure I'm missing something about setting up the development/test code.
So, with a little help from a conversation with CA Agile Central folks, it turns out part of this was an error on my part. This app does not use the Rally App Builder. It must be built using grunt instructions on the original readme.md from RallyTechServices.