Why is Fetch Undefined? - node.js

I am copying a code DIRECTLY from discord.js guide to send a direct message to myself (and only myself) through my bot.
module.exports = {
callback: async(client) => {
const user = await client.users.fetch('503965897203908609');
user.send('content');
}
}
here's the relevant code. and when I run the command, I get
TypeError: Cannot read properties of undefined (reading 'fetch')
WHY

Because your client.users is undefined, if we try to access a property on undefined it will throw an error.
as to why client.users is undefined, that would be something which requires more code to look at

Related

How to resolve client.guilds.cache.get().commands cannot read properties of undefined?

Before discordjs update my code works well but now with the update this part doesn't work.
module.exports = {
name : 'ready',
once : true,
async execute(client){
const devGuild = await client.guilds.cache.get('243406719638568960')
devGuild.commands.set(client.commands.map(cmd => cmd))
}
}
devGuild is undefined. How can I resolve the error to set my commands:
UNCAUGHT_EXCEPTION : TypeError: Cannot read properties of undefined (reading 'commands'), Origine : uncaughtException

Shopify React Next Cannot read property "/_app" of undefined

I am trying to setup an environment to develop a shopify theme, and I followed the tutorial found here:
https://shopify.dev/tutorials/build-a-shopify-app-with-node-and-react/build-your-user-interface-with-polaris
But when I connected my server, the app, the store and so on, when I started integrating polaris, on build I started getting errors.
First I had an error about a missing module build-manifest.json and I found a suggestion somewhere on the forums to make a blank file with just {} in it in the .next/ directory and so I did.
Then it complained about react-loadable-manifest.json missing, and I did the same for it.
Both of these errors were being thrown from this piece of code:
server.use(async (ctx) => {
await handle(ctx.req, ctx.res); // <-- this line
ctx.respond = false;
ctx.res.statusCode = 200;
return;
});
And now I have the following errors:
error - ./node_modules/#shopify/polaris/dist/styles.css
TypeError: Cannot read property 'tapAsync' of undefined
TypeError: Cannot read property '/_app' of undefined
Thats the full stack, I dont see any "custom" code related to it:
error - ./node_modules/#shopify/polaris/dist/styles.css
TypeError: Cannot read property 'tapAsync' of undefined
TypeError: Cannot read property '/_app' of undefined
at getPageFiles (C:\Users\Darkbound\Desktop\shopifyreact\server\node_modules\next\dist\next-server\server\get-page-files.js:1:311)
at getDocumentFiles (C:\Users\Darkbound\Desktop\shopifyreact\server\.next\server\pages\_document.js:266:54)
at Head.render (C:\Users\Darkbound\Desktop\shopifyreact\server\.next\server\pages\_document.js:589:19)
at processChild (C:\Users\Darkbound\Desktop\shopifyreact\server\node_modules\react-dom\cjs\react-dom-server.node.development.js:3450:18)
at resolve (C:\Users\Darkbound\Desktop\shopifyreact\server\node_modules\react-dom\cjs\react-dom-server.node.development.js:3270:5)
at ReactDOMServerRenderer.render (C:\Users\Darkbound\Desktop\shopifyreact\server\node_modules\react-dom\cjs\react-dom-server.node.development.js:3753:22)
at ReactDOMServerRenderer.read (C:\Users\Darkbound\Desktop\shopifyreact\server\node_modules\react-dom\cjs\react-dom-server.node.development.js:3690:29)
at renderToStaticMarkup (C:\Users\Darkbound\Desktop\shopifyreact\server\node_modules\react-dom\cjs\react-dom-server.node.development.js:4314:27)
at renderDocument (C:\Users\Darkbound\Desktop\shopifyreact\server\node_modules\next\dist\next-server\server\render.js:3:715)
at renderToHTML (C:\Users\Darkbound\Desktop\shopifyreact\server\node_modules\next\dist\next-server\server\render.js:56:103)
at async C:\Users\Darkbound\Desktop\shopifyreact\server\node_modules\next\dist\next-server\server\next-server.js:107:97
at async C:\Users\Darkbound\Desktop\shopifyreact\server\node_modules\next\dist\next-server\server\next-server.js:100:142
at async DevServer.renderToHTMLWithComponents (C:\Users\Darkbound\Desktop\shopifyreact\server\node_modules\next\dist\next-server\server\next-server.js:132:387)
at async DevServer.renderErrorToHTML (C:\Users\Darkbound\Desktop\shopifyreact\server\node_modules\next\dist\next-server\server\next-server.js:134:327)
at async DevServer.render (C:\Users\Darkbound\Desktop\shopifyreact\server\node_modules\next\dist\next-server\server\next-server.js:72:236)
at async Object.fn (C:\Users\Darkbound\Desktop\shopifyreact\server\node_modules\next\dist\next-server\server\next-server.js:56:580)
I came across this issue when jumping next multiple dependencies versions at once. I was able to resolve it by removing the wrapped Container
https://github.com/vercel/next.js/blob/master/errors/app-container-deprecated.md

TypeError: Cannot read property 'then' of null | discord.js

I am trying to make a message tracker and this error shows and I don't know why
Code: messagecounter.js
const db = require('quick.db');
module.exports = {
name: "msgc",
description: "Message Counter",
async run(client, message, args) {
// checking who wants to fetch it
let member = message.mentions.members.first() || message.member; // this checks if they mentioned a members
db.fetch(`messageSent_${member.id}`).then(obj => {
message.channel.send(`**Messages Sent:** \`${obj.value}\``);
});
}
}
Code: bot.js:70:42
client.commands.get(command).run(client, message, args);
Error:
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'then' of null
at Object.run (C:\Users\Familia\OneDrive\Documents\Other Stuff\Visual Studio code\blade\commands\messagecounter.js:13:45)
at Client.<anonymous> (C:\Users\Familia\OneDrive\Documents\Other Stuff\Visual Studio code\blade\bot.js:70:42)
Any help would be appreciated
Using discord.js v12
After quickly glancing over "quick.db" I couldn't find a method called fetch defined on the db object. "get", however is defined and is perhaps what you meant to use.

Koa cannot get the value of the property inside ctx.request-body

Koa cannot get the value of the property inside ctx.request-body
Project koa is generated by koa-generator
Routing section related code
Either require('koa-body') or require('koa-bodyparser')
console.log("ctx")
console.log(ctx.request.body)
console.log(ctx.request.body.type)
})
The three console.log prints are
ctx
{
account:'root',
password: 'test',
type:0
}
undefined
I can get the object inside the ctx.requisition.body and print it out, but ctx.request.body.type is undefined
How to get 'ctx.requisition.body.account' or 'ctx.requisition.body.password' ?
Maybe if you do this
const myObj = JSON.parse(ctx.request.body)
console.log(myObj.type)
You'll get ctx.request.body.type

Node.js Callback Failing - Function pass Variables with Callback

I'm new to Node.js and I'm struggling to get a callback to work. I have the following function call:
memberPhotoPath(dbResults[i].userid2,dbResults[i].userid2Gender,'small',dbResults[i].userid2PhotoName,dbResults[i].userid2PhotoVerified,false,function(path) {
console.log(path);
});
and the following function:
function memberPhotoPath(userid,gender,photoSize,photoName,photoVerification,border,callback) {
if(photoVerification) {
callback('http://www.datingimages.co/online-dating/dating-photos/'+userid+'/'+userid+'-'+photoSize+'-'+photoName+'.jpg');
}else{
if(border) {
if(gender) {
callback('http://www.datingimages.co/online-dating/dating-website/default-female-image-'+photoSize+'.png');
}else{
callback('http://www.datingimages.co/online-dating/dating-website/default-male-image-'+photoSize+'.png');
}
}else{
if(gender) {
callback('http://www.datingimages.co/online-dating/dating-website/default-female-image-'+photoSize+'-noborder.png');
}else{
callback('http://www.datingimages.co/online-dating/dating-website/default-male-image-'+photoSize+'-noborder.png');
}
}
}
}
I get the following error in Node.js:
TypeError: undefined is not a function
at memberPhotoPath (/etc/node/index.js:315:5)
at /etc/node/index.js:223:21
at memberPhotoPath (/etc/node/index.js:315:5)
at /etc/node/index.js:214:9
at Array.forEach (native)
at /etc/node/index.js:208:34
at Query._callback (/etc/node/index.js:287:9)
at Query.Sequence.end (/usr/lib/node_modules/mysql/lib/protocol/sequences/Sequence.js:75:24)
at Query._handleFinalResultPacket (/usr/lib/node_modules/mysql/lib/protocol/sequences/Query.js:143:8)
at Query.EofPacket (/usr/lib/node_modules/mysql/lib/protocol/sequences/Query.js:127:8)
Any advice on what I'm doing wrong?
thankyou
I have just run this code in the chrome console and it works fine. The error must be somewhere down the line. With asynchronous functions it is sometimes not possible to track the exact stack trace. I advise you brace yourself and go through your other code again

Resources