Javascript 如何将数据库中的数据应用于Three.js交互对象

Javascript 如何将数据库中的数据应用于Three.js交互对象,javascript,json,three.js,data-visualization,Javascript,Json,Three.js,Data Visualization,我试图为数据库中的每个条目呈现一个对象,并根据每个对象的相对数据库条目向其附加URL、标题和描述等文本 到目前为止,在场景中显示的每个条目都有一个立方体,单位为DB,但是数据记录为未定义。从我所看到的情况来看,我的代码应该可以正常工作,但事实并非如此 getData() async function getData() { try { var response = await fetch('/api/indexvr');

我试图为数据库中的每个条目呈现一个对象,并根据每个对象的相对数据库条目向其附加URL、标题和描述等文本

到目前为止,在场景中显示的每个条目都有一个立方体,单位为DB,但是数据记录为未定义。从我所看到的情况来看,我的代码应该可以正常工作,但事实并非如此

  getData()

  async function getData() {
        try {
            var response = await fetch('/api/indexvr');
            var data = await response.json();


            for (var i = 0; i < data.length; i++) {
                cube = new THREE.Mesh(geometry, material.clone());
                cube.userData = {
                    id: data.id,
                    URL: `/vr/${data.id}/`,
                    title: data.title, 
                    description: data.description
                    };
                cube.position.x = i;
                scene.add(cube);
                console.log(cube.userData) 
                //group.add(data)
            }
        } catch (e) {
            console.error(e)
        }
  }
getData()
异步函数getData(){
试一试{
var response=wait fetch('/api/indexvr');
var data=await response.json();
对于(变量i=0;i
上面是获取我的MongoDB集合中的每个条目的代码(它是文件路径和数据的列表,如文件标题、描述和标签)

从我发现的例子来看,“userData”似乎是一个不错的选择

目前,我可以将鼠标悬停在对象上,它们通过光线投射改变颜色。在将来,一旦我越过了这个障碍,我将创建一种方法来查看文本数据,例如当对象悬停在上面时的标题,并在单击时指向URL


我不熟悉three.js,这让我很困惑。

我认为您混淆了
数据的结构。有时将其视为对象,有时将其视为数组

例如,如果在For()循环中使用
data.length
,则
data
是一个数组。您无法访问数组的
.description
.title
,但您可以使用以下命令访问数组中的每个元素:
数据[0]。描述,数据[1]。描述

// Create variable to make a reference
// to the current element while looping in the array
var arrayElement;

for (var i = 0; i < data.length; i++) {
    // Get the element for this iteration
    arrayElement = data[i];

    cube = new THREE.Mesh(geometry, material.clone());

    // Now you can acces the information in the current array element
    cube.userData = {
        id: arrayElement.id,
        URL: `/vr/${arrayElement.id}/`,
        title: arrayElement.title, 
        description: arrayElement.description
    };
    cube.position.x = i;
    scene.add(cube);
    console.log(cube.userData)
}
//创建变量以进行引用
//在数组中循环时返回到当前元素
var阵列;
对于(变量i=0;i
我认为您混淆了
数据的结构。有时将其视为对象,有时将其视为数组

例如,如果在For()循环中使用
data.length
,则
data
是一个数组。您无法访问数组的
.description
.title
,但您可以使用以下命令访问数组中的每个元素:
数据[0]。描述,数据[1]。描述

// Create variable to make a reference
// to the current element while looping in the array
var arrayElement;

for (var i = 0; i < data.length; i++) {
    // Get the element for this iteration
    arrayElement = data[i];

    cube = new THREE.Mesh(geometry, material.clone());

    // Now you can acces the information in the current array element
    cube.userData = {
        id: arrayElement.id,
        URL: `/vr/${arrayElement.id}/`,
        title: arrayElement.title, 
        description: arrayElement.description
    };
    cube.position.x = i;
    scene.add(cube);
    console.log(cube.userData)
}
//创建变量以进行引用
//在数组中循环时返回到当前元素
var阵列;
对于(变量i=0;i