Client is not defined - javascript

I am trying to host a discord bot on my ubuntu server.
Everything was working but for some reason it just stopped working and it shows the same error all the time, no matter what i change.
const Discord = require('discord.js');
const client = new Discord.Client({intents: ["MessageContent"]});
client.login("My bots token")
console.log(client);
client.on('message', message => {
if (message.content === '!hello') {
message.channel.send('Nazdar!');
}
})
client.once('ready', () => {
console.log('Ready!');
})
The error message:
> 0|index | at Module._compile (node:internal/modules/cjs/loader:1275:14)
> 0|index | at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
> 0|index | at Module.load (node:internal/modules/cjs/loader:1133:32)
> 0|index | at Module._load (node:internal/modules/cjs/loader:972:12)
> 0|index | ReferenceError: Client is not defined
> 0|index | at Object.<anonymous> (/root/pives-bot/index.js:5:1)
> 0|index | at Module._compile (node:internal/modules/cjs/loader:1275:14)
> 0|index | at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
> 0|index | at Module.load (node:internal/modules/cjs/loader:1133:32)
> 0|index | at Module._load (node:internal/modules/cjs/loader:972:12)
> 0|index | at Object.<anonymous> (/root/.nvm/versions/node/v19.6.1/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23)
> 0|index | at Module._compile (node:internal/modules/cjs/loader:1275:14)
> 0|index | at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
> 0|index | at Module.load (node:internal/modules/cjs/loader:1133:32)
> 0|index | at Module._load (node:internal/modules/cjs/loader:972:12)
>
I tried changing all forms of uppercase or lowercase characters, but still the same issue. Thanks for every help!!

Related

Not able to solve "ReferenceError: Client is not defined" error

I am getting this error which I do not understand and am not able to fix.
The code is,
const fs = require('fs')
const Web3 = require('web3')
const { NonceTxMiddleware, SignedTxMiddleware, LocalAddress, CryptoUtils, HDWalletProvider } = require('#truffle/hdwallet-provider')
const client = new Client (
goerliChainId,
"https://eth-goerli.g.alchemy.com/v2/zEsvEP-zdgrdgdffgfgf64kuugnfvd6P",
"https://eth-goerli.g.alchemy.com/v2/oqB3MrwE1sdfgsdfgsdfgy5WD71Qj24n"
)
The error message is,
const client = new Client (
^
ReferenceError: Client is not defined
at Object.loadAccount (C:\Users\VidhanMangla\Desktop\mqube-blockchain-oracle\utils\common.js:13:20)
at init (C:\Users\VidhanMangla\Desktop\mqube-blockchain-oracle\Oracle.js:128:53)
at C:\Users\VidhanMangla\Desktop\mqube-blockchain-oracle\Oracle.js:138:60
at Object.<anonymous> (C:\Users\VidhanMangla\Desktop\mqube-blockchain-oracle\Oracle.js:149:3)
at Module._compile (node:internal/modules/cjs/loader:1218:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
at Module.load (node:internal/modules/cjs/loader:1081:32)
at Module._load (node:internal/modules/cjs/loader:922:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47
I am not able to understand anything, please help.

TypeError: Cannot read properties of undefined (reading 'eth')

const {web3js, myAccount} = require ('./utils')
const {bytecode} = require ('./contractartifact')
async function deploy() {
web3js.eth.sendTransaction({
from: myAccount.address,
data: bytecode,
gas: 800
})
.on('receipt', console.log)
}
deploy()
Error
C:\Users\giris\getter-setter\deploy.js:9
web3js.eth.sendTransaction({
^
TypeError: Cannot read properties of undefined (reading 'eth')
at deploy (C:\Users\giris\getter-setter\deploy.js:9:12)
at Object. (C:\Users\giris\getter-setter\deploy.js:19:1)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules /cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47
Your import is incorrect here:
const {web3js, myAccount} = require ('./utils')
It should be:
const web3js = new Web3(Web3.givenProvider || "ws://localhost:8545");

UnhandledPromiseRejectionWarning: RequestError: Error: Invalid URI "0"

I am new to nodejs and trying to write this web scraper where I am getting the following errors. It asks to return promise however I tried but nothing works out. Not sure if I am using the right packages. Promises in async is quite difficult to understand for me at this point. Any explanation along with the code will be really apprciated.
PS C:\Users\farid\Desktop\Node Projects\webscraping> node --trace-warnings .\index.js (node:10748) UnhandledPromiseRejectionWarning: RequestError: Error: Invalid URI "0"
at new RequestError (C:\Users\farid\Desktop\Node Projects\webscraping\node_modules\request-promise-core\lib\errors.js:14:15)
at Request.plumbing.callback (C:\Users\farid\Desktop\Node Projects\webscraping\node_modules\request-promise-core\lib\plumbing.js:87:29)
at Request.RP$callback [as _callback] (C:\Users\farid\Desktop\Node Projects\webscraping\node_modules\request-promise-core\lib\plumbing.js:46:31)
at self.callback (C:\Users\farid\Desktop\Node Projects\webscraping\node_modules\request\request.js:185:22)
at Request.emit (events.js:315:20)
at Request.init (C:\Users\farid\Desktop\Node Projects\webscraping\node_modules\request\request.js:273:17)
at Request.RP$initInterceptor [as init] (C:\Users\farid\Desktop\Node Projects\webscraping\node_modules\request-promise-core\configure\request2.js:45:29)
at new Request (C:\Users\farid\Desktop\Node Projects\webscraping\node_modules\request\request.js:127:8)
at request (C:\Users\farid\Desktop\Node Projects\webscraping\node_modules\request\index.js:53:10)
at C:\Users\farid\Desktop\Node Projects\webscraping\index.js:13:26
at Object.<anonymous> (C:\Users\farid\Desktop\Node Projects\webscraping\index.js:50:3)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
at emitUnhandledRejectionWarning (internal/process/promises.js:168:15)
at processPromiseRejections (internal/process/promises.js:247:11)
at processTicksAndRejections (internal/process/task_queues.js:94:32)
(node:10748) RequestError: Error: Invalid URI "0"
at new RequestError (C:\Users\farid\Desktop\Node Projects\webscraping\node_modules\request-promise-core\lib\errors.js:14:15)
at Request.plumbing.callback (C:\Users\farid\Desktop\Node Projects\webscraping\node_modules\request-promise-core\lib\plumbing.js:87:29)
at Request.RP$callback [as _callback] (C:\Users\farid\Desktop\Node Projects\webscraping\node_modules\request-promise-core\lib\plumbing.js:46:31)
at self.callback (C:\Users\farid\Desktop\Node Projects\webscraping\node_modules\request\request.js:185:22)
at Request.emit (events.js:315:20)
at Request.init (C:\Users\farid\Desktop\Node Projects\webscraping\node_modules\request\request.js:273:17)
at Request.RP$initInterceptor [as init] (C:\Users\farid\Desktop\Node Projects\webscraping\node_modules\request-promise-core\configure\request2.js:45:29)
at new Request (C:\Users\farid\Desktop\Node Projects\webscraping\node_modules\request\request.js:127:8)
at request (C:\Users\farid\Desktop\Node Projects\webscraping\node_modules\request\index.js:53:10)
at C:\Users\farid\Desktop\Node Projects\webscraping\index.js:13:26
at Object.<anonymous> (C:\Users\farid\Desktop\Node Projects\webscraping\index.js:50:3)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
(node:10748) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
at emitDeprecationWarning (internal/process/promises.js:180:11)
at processPromiseRejections (internal/process/promises.js:249:13)
at processTicksAndRejections (internal/process/task_queues.js:94:32)
PS C:\Users\farid\Desktop\Node Projects\webscraping>
Here is my index.js file code.
const request = require("request-promise");
const cheeri = require("cheerio");
const fs = require("fs")
const json2csv = require("json2csv").Parser;
const web_urls = ["https://www.gillmanacura.com/" , "https://www.gillmanacura.com/service/contact-service/"];
// const web_urls = ["https://www.fernandezhonda.com/trade-in-calculator/"];
(async() => {
let emptyData = [];
for(let web_url in web_urls){
let resp = await request({
uri: web_url,
headers:{},
timeout:10000,
json: true,
gzip: true
});
let $ = cheeri.load(resp)
let title = $('head title').text()
let desc = $('meta[name="description"]').attr('content')
let canonical = $('link[rel="canonical"]').attr('href')
let kwd = $('meta[name="keywords"]').attr('content')
let ogTitle = $('meta[property="og:title"]').attr('content')
let ogImage = $('meta[property="og:image"]').attr('content')
let ogkeywords = $('meta[property="og:keywords"]').attr('content')
emptyData.push({
title,
desc,
canonical,
kwd,
ogTitle,
ogImage,
ogkeywords
});
}
const j2csv = new json2csv()
const csv = j2csv.parse(emptyData)
fs.writeFileSync("./results.csv", csv, "utf-8");
})();
How can I resolve these errors? Please advise.
Use for of instead of in. in will iterate over the keys and of over the values.
for(let web_url in web_urls){

Mongoose heartbeat and reconnect don't work

the automatic reconnection function natively embedded in mongoose does not work on my project.
I actually receive a return in error but it does not start again after the X seconds that I configured.
Beyond whether the credentials are correct or not, I expect mongoose to try to log in every X seconds (and therefore send me a log each time) but it is not
Here is the doc in question: https://mongoosejs.com/docs/connections.html#options
Here is my code:
import mongoose from "mongoose"
const options = {
dbName: "testbdd",
pass: "qwe",
user: "alex",
useNewUrlParser: true,
serverSelectionTimeoutMS: 1000,
heartbeatFrequencyMS: 1000,
useUnifiedTopology: true
}
const uri = `mongodb://localhost/`
mongoose.connection.on('disconnected', function () {
console.log('Mongo db disconnected!');
});
mongoose.connect(uri, options).then(() => {
console.log('First Mongo db connexion success !')
}).catch((error) => {
console.log('First Mongo db connexion fail !')
console.log(error)
})
Here is the return when I run the node server:
backend_1 | Mongo db disconnected!
backend_1 | First Mongo db connexion fail !
backend_1 | MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
backend_1 | at NativeConnection.Connection.openUri (/user/app/node_modules/mongoose/lib/connection.js:800:32)
backend_1 | at /user/app/node_modules/mongoose/lib/index.js:341:10
backend_1 | at /user/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
backend_1 | at new Promise (<anonymous>)
backend_1 | at promiseOrCallback (/user/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
backend_1 | at Mongoose.connect (/user/app/node_modules/mongoose/lib/index.js:340:10)
backend_1 | at Object.connect (/user/app/src/db/mongo/index.js:28:10)
backend_1 | at Module._compile (internal/modules/cjs/loader.js:776:30)
backend_1 | at Module._compile (/user/app/node_modules/pirates/lib/index.js:99:24)
backend_1 | at Module._extensions..js (internal/modules/cjs/loader.js:787:10)
backend_1 | at Object.newLoader [as .js] (/user/app/node_modules/pirates/lib/index.js:104:7)
backend_1 | at Module.load (internal/modules/cjs/loader.js:643:32)
backend_1 | at Function.Module._load (internal/modules/cjs/loader.js:556:12)
backend_1 | at Module.require (internal/modules/cjs/loader.js:683:19)
backend_1 | at require (internal/modules/cjs/helpers.js:16:16)
backend_1 | at Object.require (/user/app/src/bin/www.js:7:1) {
backend_1 | message: 'connect ECONNREFUSED 127.0.0.1:27017',
backend_1 | reason: TopologyDescription {
backend_1 | type: 'Single',
backend_1 | setName: null,
backend_1 | maxSetVersion: null,
backend_1 | maxElectionId: null,
backend_1 | servers: Map { 'localhost:27017' => [ServerDescription] },
backend_1 | stale: false,
backend_1 | compatible: true,
backend_1 | compatibilityError: null,
backend_1 | logicalSessionTimeoutMinutes: null,
backend_1 | heartbeatFrequencyMS: 1000,
backend_1 | localThresholdMS: 15,
backend_1 | commonWireVersion: null
backend_1 | }
backend_1 | }
Those options should work well, no ?
serverSelectionTimeoutMS: 1000,
heartbeatFrequencyMS: 1000,
useUnifiedTopology: true
I am really lost. I doubt it's the mongoose code that doesn't work, it's necessarily my implementation.
Can you help me ?
This is the intended behavior of the mongodb driver
https://jira.mongodb.org/browse/NODE-834?focusedCommentId=1412582&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-1412582
The driver will fail on first connect if it cannot connect to the host. This is by design to ensure quick failure on unreachable hosts. Reconnect behavior only kicks in once the driver has performed the initial connect.

TypeError: response.redirect is not a function

I need help...
So, I have this code
var express = require('express');
var app = express();
//Sites
// Landingpage CourseWorkers ----------------------------------------
app_courseworkers = require('./sites/app')
app_https_courseworkers = (req, response) => response.redirect("https://courseworkers.com")
But when I run it on the server I get the message:
0|index | TypeError: response.redirect is not a function
0|index | at Object.app_https_courseworkers [as handler] (/var/www/html/courseworkers/index.js:9:55)
0|index | at Server.overallHandler (/var/www/html/courseworkers/node_modules/vhttps/lib/init.js:103:25)
0|index | at emitTwo (events.js:126:13)
0|index | at Server.emit (events.js:214:7)
0|index | at parserOnIncoming (_http_server.js:619:12)
0|index | at HTTPParser.parserOnHeadersComplete (_http_common.js:115:23)
Can anyone point out what I'm doing wrong?
Note: Running in localhost it goes normally
If you are trying to redirect to an external URL when someone goes to a specific URL on your site, try this:
app.get("/course", (req, res) => {
res.redirect("https://courseworkers.com");
});
My guess is, you are trying to call app_https_courseworkers(req, response), but you aren't actually passing in a valid response object.

Categories