无法在javascript中使用mongodb的变量字段

问题描述 投票:0回答:1

我从 mongodb 检索了一份文档,因为我有一个数组,我想循环遍历该数组,但我不能这样做。如果我循环遍历它,它将整个数组视为一个元素,这就是我的代码:

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');
}

最近的输出:

{
  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

尝试循环数组我已经做到了

var obj;
project.variables.forEach((var) => {
   obj[var]=""
}

我得到了这个输出:

[['var1','var2','var3']]:""

这是预期的输出:

{
   'var1':"",
   'var2':"",
   'var3':"",
}
javascript arrays mongodb mongoose
1个回答
0
投票

您是否尝试以这种方式分解您的数据:

const { variables } = data;

然后这样循环:

variables.forEach(variable => {
console.log(variable); 
});

最后你可以这样做:

const { variables } = data;
const obj = {};
variables.forEach(variable => {
obj[variable] = '';
});
console.log(obj);

输出:{ var1: '', var2: '', var3: '' }

© www.soinside.com 2019 - 2024. All rights reserved.