I'm facing an issue while trying to loop through an array I retrieved from mongodb. It seems the array is treated as a single element, and here is the code snippet:
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');
}
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
Attempted to loop through array using the following code:
var obj;
project.variables.forEach((var) => {
obj[var]=""
}
Output received:
[['var1','var2','var3']]:""
Expected output:
{
'var1':"",
'var2':"",
'var3':"",
}