I have retrieved one document from mongodb, in that I have one array and I want to loop through that array but I can’t do that. If I loop through that it consider whole array as one element, this is my code:
const project = await Project.findOne({
embedId: inititalData.embedId
});
console.log(project.variables,typeof project.variables,project.variables[0], "project")
const variables = project.variables;
console.log('Variables array:', variables);
// Check type and individual elements
console.log('Type of variables:', typeof variables);
if (variables instanceof Array) {
console.log('First variable:', variables[0]);
} else {
console.error('Variables is not an array');
}
Very recent output:
{
nodes: [
{
id: 'start101',
type: 'startNode',
data: [Object],
position: [Object],
width: 80,
height: 40,
selected: false,
positionAbsolute: [Object],
dragging: false
},
{
id: 'Group-XUPJ',
type: 'chatbotCommand',
position: [Object],
data: [Object],
width: 260,
height: 190,
selected: true,
positionAbsolute: [Object],
dragging: false
},
{
id: 'Group-8q8Z',
type: 'chatbotCommand',
position: [Object],
data: [Object],
width: 260,
height: 190,
selected: false,
positionAbsolute: [Object],
dragging: false
}
],
variables: [ 'var1', 'var2', 'var3' ],
_id: new ObjectId('66a1d83534c4d42a3d5ae07e'),
userId: 'user_2imJECN4mQmEz6p00ZeM9VTZAVt',
name: 'asdfsdf',
edges: [
{
source: 'start101',
sourceHandle: 'a',
target: 'Group-8q8Z',
targetHandle: 'b',
type: 'smoothstep',
animated: true,
className: 'border-2 border-gray-300',
id: 'reactflow__edge-start101a-Group-8q8Zb'
},
{
source: 'Group-8q8Z',
sourceHandle: 'a',
target: 'Group-XUPJ',
targetHandle: 'b',
type: 'smoothstep',
animated: true,
className: 'border-2 border-gray-300',
id: 'reactflow__edge-Group-8q8Za-Group-XUPJb'
},
{
source: 'Group-XUPJ',
sourceHandle: 'a',
target: 'Group-8q8Z',
targetHandle: 'b',
type: 'smoothstep',
animated: true,
className: 'border-2 border-gray-300',
id: 'reactflow__edge-Group-XUPJa-Group-8q8Zb'
}
],
aiPrompts: [],
aiModel: 'GPT-3.5',
isScriptTagAvailable: true,
createdAt: 2024-07-25T04:44:37.511Z,
updatedAt: 2024-07-25T17:45:25.080Z,
__v: 0,
embedId: 9584913626795330,
scriptTag: '<script async data-id="9584913626795330" id="taskDeno-embed-script" type="text/javascript" src="https://task-deno.vercel.app/js/embed.js">n' +
'</script>'
}
👇project.variables 👇typeof project.variables 👇 project.variables[0]
[ 'var1', 'var2', 'var3' ] object undefined project
Variables array: [ 'var1', 'var2', 'var3' ]
Type of variables: object
Variables is not an array
Tried to loop through array I have done this
var obj;
project.variables.forEach((var) => {
obj[var]=""
}
I got this output:
[['var1','var2','var3']]:""
Here is the expected output:
{
'var1':"",
'var2':"",
'var3':"",
}
2
Answers
This issue was created by mongoose, which was wrapping the object before I use it. I fixed it with:
Did you try to decompose your data this way:
And then loop over this way:
Then at the end you could do this:
Output: { var1: ”, var2: ”, var3: ” }