nestJS undefined in a foreach loop - nestjs

let allDB = await this.findAll();
//allDB output in console shows it has everything from db as I want
allDB .forEach(item => {
//item is undefined here
});
async findAll() {
return this.db.find()
}
I'm trying to load everything from a mongo collection and after that do a foreach loop trought it, but item is always undefined.
Thanks for your help

In the code you provided us there is a space between 'allDB' and '.forEach'. Is it the case in your project ? Because it can be the source of your error. Remove it to be sure it's not this.
I don't think that's the problem because it should throw a syntax error but it's still worth trying.

Related

Impossible to rename a member from its id

I am merging a bot under discord.js v12 to a newer bot with discord.js v14.
However it's my first experience on the node and I don't understand everything, I'm doing well in lua but I don't understand...
The events return me errors either it does not find the guild or it is impossible to rename a player, or a player is not found...
Here is how it is, I comment you the events according to the return of log that I have
on("DiscordBot:RenameMember", (guild, discordid, fullname) => {
let targetguild = client.guilds.cache.first(); // I got on log the name of GUILD
if (targetguild) {
let target = targetguild.members.fetch(discordid); // I got on log : [object Promise]
if (target) {
target.setNickname(fullname); // I got on log : TypeError: target.setNickname is not a function
target.member.setNickname(fullname); // I got on log : TypeError: Cannot read properties of undefined (reading 'setNickname')
}
}
});
I made a multitude of tests with cache, without cache but nothing makes it all I find is similar and does not work here...
Here you will find the launch of the bot :
https://hastebin.com/ovipelopam.js
And here is the file I made separately to find my way around :
https://hastebin.com/azaraqituh.js
Do you have an idea ?
In your case targetguild.members.fetch(discordid) seems to return a Promise, which is an async operation. Therefore target has no value right away, but instead, has to either be awaited, or the rest of the code has to be put in the then function, that can be set to the promise.
Here is an example where you await the promise to resolve. In order to achieve this you have to define your function as async, so that it itself returns a Promise:
on("DiscordBot:RenameMember", async (guild, discordid, fullname) => {
let targetguild = client.guilds.cache.first(); // I got on log the name of GUILD
if (targetguild) {
let target = await targetguild.members.fetch(discordid); // I got on log : [object Promise]
if (target) {
target.setNickname(fullname); // I got on log : TypeError: target.setNickname is not a function
target.member.setNickname(fullname); // I got on log : TypeError: Cannot read properties of undefined (reading 'setNickname')
}
}
});
Thank you for answering and explaining,
I still don't understand how it works, but it will come...
I had some problems with the setNickname and it started to work without understanding why either :)
Here is what I get in the end :
on("DiscordBot:RenameMember", async (guild, discordid, fullname) => {
let targetguild = client.guilds.cache.first();
if (targetguild) {
let target = await targetguild.members.fetch(discordid);
if (target) {
// target.setNickname(fullname); // I got on log : TypeError: target.setNickname is not a function
// target.members.setNickname(fullname); // I got on log : TypeError: Cannot read properties of undefined (reading 'setNickname')
let targetRename = await target.setNickname(fullname, 'Modification correspondant au profile InGame'); // That work
}
}
});
Thanks again :)

why is my error handling not working in express js?

i just need someone to tell me what is wrong with my code. This is it.
const getSingletask = async (req, res)=>
{
try{
const task = await Task.findOne({_id:req.params.id})
if(!task){
return res.json({msg:`No such task with and id of ${req.params.id}`})
}
res.json({task})
}catch(error){
res.json({msg:error})
}
}
Basically what the code is doing is that whenever the function excutes, it finds one particular item using its ID. The IF statement is the first error handler. It is meant to return that message if the ID cannot be found but it keeps bringing up an error anytime i test it out. I think my code is correct so can someone tell me what i did wrong?
You seem to be using mongoose. Mongoose saves db objects by way of a wrapper called ObjectId, that accepts exactly 24 hex characters. Because the ID you are inputting into it is not 24 hex characters, mongoose fails to cast it (meaning to use the wrapper), and so, throws an error.

Collecting every guild and members returns "this.member.get" is not a function

So I have this discord bot and since I'm personally hosting it from my pc with nodejs I wanted to make the bot update the members list on my webapi everytime I boot it back up.
Problem is that Using the code below the console returns an error saying the member.get is not a function.
var timestamp=Date.now(), json={timestamp:timestamp, guilds:[]}, count=0;
try{
client.guilds.forEach(guild => {
json.guilds.push(guild);
var list = guild.members;
json.guilds[count].members=[];
list.forEach(member => json.guilds[count].members.push(member.user.id));
count++;
});
} catch(e) {
console.log(e);
}
The error:
TypeError: this.members.get is not a function
Anyone can help?
Thanks in advance.
Since Discord.js v12 you need to use the cache:
So instead of...
guild.members.get();
...you have to do this:
guild.members.cache.get();

Updating a retrieved Firestore document?

I'm stumbling onto an issue which I find silly, but can't seem to get around:
I'm not directly able to update the documents which I retrieve from Firestore. For example, when I try the deploy the following code within a onWrite trigger to a different node:
admin.firestore().collection("user-data").doc('someUserId').get().then(doc => {
const profile = doc.data()
if (profile.foo != 'bar') {
return 0
}
return doc.update({
objectToUpdate: {
fieldToUpdate: 'Foo is not bar!'}
})
I get the error that doc.update is not a function
I've also tried doc.ref.update, and doc.data.ref.update, but no dice.
I can achieve what I want with admin.firestore().collection("user-data').doc('someUserId').update({...}), but that just feels so clunky...
What am I missing here?

Meteor ReactiveDict MongoDB Find onCreate

I'm trying to use a mongodb find items and stored in ReactiveDict, but I'm just getting the error:
{{#each}} currently only accepts arrays, cursors or falsey...
What am I doing wrong here?
Template.body.onCreated(function(){
Meteor.subscribe('itemFind');
this.global = new ReactiveDict();
this.global.set('items',Items.find());
});
Template.body.helpers({
items(){
console.log(Template.instance().global.get('items'));
return Template.instance().global.get('items');
}
});
Further, I figured if I added a .fetch() to the original find statement this would be fixed, but apparently not.
I'm new to Meteor, so what am I doing wrong here?
On the onCreated the subscription is not ready yet. A good practice is to define your Reactive vars on onCreated and assign them on onRendered.
In any case, you need to wait for the subscription to be ready. To do so you can use an autorun(). The autorun() will rerun every time its dependencies are updated.
Template.body.onCreated(function() {
this.global = new ReactiveDict({});
});
Template.body.onRendered(function()
this.autorun(() => {
const subs = Meteor.subscribe('itemFind');
if(subs.ready()) {
this.global.set('items', Items.find({}));
}
});
});

Resources