Hello Im getting error: Cannot read properties of undefined (reading 'split').
When I see it in the console
If I click the link to the error it shows me the browser.umd.js file with this line:
any one knows whats the problem?
code:
const verifyJwt = (req,res,next) => {
console.log('entered middle');
let name = 'token=';
const header = req.headers['cookie'];
const token = header && header.split('=')[1];
if (!token) return res.status(401).send({msg: "Login first please!"})
jwt.verify(token, process.env.TOKEN_KEY, (err, user) => {
if(err) return res.status(403).send({msg:"Not authoraized"})
next();
});
}
module.exports = verifyJwt
The problem occurs because of an other error that was found in the code but havent notice, because of this the string was undefined value and does not have the func split.
Related
I tried to get data using filter in nodejs and firestore when show me this error in postman
"Cannot read properties of undefined (reading '_internalPath')"
my code
const getAllWords = async (req, res, next) => {
let categoryName = "fruits"
try {
const word = await firestore.collection('12words');
const data = await word.where({categoryName}).get();
const wordArray = [];
if(data.empty) {
res.status(404).send('No words12 found');
}else {
data.forEach(doc => {
const words = new Word12(
doc.id,
doc.data().name,
doc.data().categoryName,
);
wordArray.push(words);
});
res.send(wordArray);
}
} catch (error) {
res.status(400).send(error.message);
}
}
The .where() takes 3 parameters (fieldPath, opStr, value) but you are passing only 1 object. Try refactoring the code as shown below:
// replace fieldName with the field name in document
const data = await word.where("fieldName", "==", categoryName).get();
My code:
module.exports = async (req, res, next) => {
const redirectURL = ((req.originalUrl.includes("login") || req.originalUrl === "/") ? "/selector" : req.originalUrl);
if(req.session.user){
return next();
} else {
const state = Math.random().toString(36).substring(5);
req.client.states[state] = redirectURL;
return res.redirect(`/api/login?state=${state}`);
}
};
Give me this error:
Cannot set properties of undefined (setting 'yhgrz0jg')
Can anyone help me I've tried everything and I can't solve it?
req.client.states is undefined, that's why you're getting error. Why are you setting it anyway, since you're not using that value anywhere later on?
Hello, I had this problem like 1 week ago, and still can't solve it. So, I'm trying to do per-server prefixes, but when I boot up the bot, it sends the boot-up confirmation message, but then it says the error and it shuts down. Here is the error:
guildPrefixes[guildId] = result.prefix
^
TypeError: Cannot read property 'prefix' of null
I can't see anything wrong here, but anyways, here is the code:
module.exports.loadPrefixes = async (client) => {
await mongo().then(async (mongoose) => {
try {
for (const guild of client.guilds.cache) {
const guildId = guild[1].id
const guildPrefixes = {}
const result = await commandPrefixSchema.findOne({_id: guildId})
guildPrefixes[guildId] = result?.prefix || dprefix
console.log(result)
}
console.log('[INFO] Prefixes have been loaded')
} finally {
mongoose.connection.close()
}
})
}
I am trying to build a voice assistant news app using Alan AI. In my code, I have used newsapi.org API. When I am trying to execute the code, it is showing me 2 errors. "uncaught user exception" and "Cannot read property 'length' of undefined". If possible someone pleas help on how to get rid off this error. Here's my code
intent('What does this app do?', 'What can I do here?',
reply('This is a news project.'));
const API_KEY = 'e1f728171ee54ccd861576421b6a9fbc';
let savedArticles = [];
intent('Give me the news from $(source* (.*))', (p) => {
let NEWS_API_URL = `https://newsapi.org/v2/top-headlines?apiKey=${API_KEY}`;
if(p.source.value){
NEWS_API_URL = `${NEWS_API_URL}&sources=${p.source.value.toLowerCase().split(" ").join('-')}`
}
api.request(NEWS_API_URL, (error, response, body) => {
const {articles} = JSON.parse(body);
if(!articles.length) {
p.play('Sorry, please try searching for news from a different source');
return;
}
savedArticles = articles;
p.play({ command: 'newHeadlines', articles });
p.play(`Here are the (latest|recent) ${p.source.value}.`);
});
})
I just recently got into this, the problem is that when you try to call the news from CNN or ESPN (words that doesn't have that "-" hyphen symbol) it says undefined,
Try calling BBC-news or ABC-news first then CNN or any other news feed, that's what worked for me
Just you need to update api request method.
This code works perfectly, thanks.
let NEWS_API_URL = `https://newsapi.org/v2/top-headlines?apiKey=${API_KEY}`;
if(p.source.value){
NEWS_API_URL=`${NEWS_API_URL}&sources=${p.source.value.toLowerCase().split(" ").join('-')}`;
}
api.request.get(NEWS_API_URL, (err, res, body) => {
const {articles,totalResults} = JSON.parse(body);
if(!totalResults){
p.play('sorry, Please try searching news from different sources.!')
return;
}
p.play({command:'newHeadLines',articles});
p.play(`Here are the total ${totalResults} latest news.`);
});
This is due to the UserAgentMissing error therefore the articles are not set correctly. Here is the solution:
const options = {
url: NEWS_API_URL,
headers: {'User-Agent': 'request'}
};
api.request(options, (error, response, body) => {...}
I am trying to make a get request from client side to access data from server.js and I am getting error that says: error SyntaxError: Unexpected token < in JSON at position 0 and the url doesn't exist
which means the url is wrong or the get route is wrong, so I checked my get route and here it is:
const fakeData={animal:'lion',fact:"lions are fun"};
function getFakeData(req,res){
res.send(fakeData)
console.log(fakeData)
}
app.get('fakeAnimalData',getFakeData)
and in the terminal I can see nothing appearing
now in the client-side the get request is made like that:
document.getElementById('generate').addEventListener('click', performAction);
function performAction(e){
const newAnimal = document.getElementById('animal').value;
getAnimal(baseURL,newAnimal, apiKey)
}
const getAnimal = async (baseURL, animal, key)=>{
const res = await fetch('/fakeAnimalData')
try {
const data = await res.json();
console.log(data)
return data;
} catch(error) {
console.log("error", error);
// appropriately handle the error
}
}
here's a sandbox for the problem:https://codesandbox.io/s/loving-resonance-fjgh1?file=/src/index.js
did you try putting a slash before the 'fakeAnimalData' like this: app.get('/fakeAnimalData',getFakeData)?