WIX.com主键用作参考

时间:2018-10-10 23:11:44

标签: wixcode

我是WIX Web开发平台的新手。

我需要使用表1的主键作为表2的引用,但是我不能为此目的使用表1的ID。

我想知道最好的方法是“复制”此表1的“标题”字段(主键)中的ID。我该怎么做?这是最好的方法吗?

谢谢你, 阿图罗

1 个答案:

答案 0 :(得分:0)

Arturo:

您是否尝试过不使用wix代码来执行此操作? 查看this post,看是否可行。

现在,在代码中从另一个数据集合添加引用字段的唯一方法是使用ID。但是请注意,ID是仪表板和“编辑器”视图的数据收集中使用的字段名称。在代码中访问ID值时,需要使用 _id 字段键。

因此,在Table2中,您需要一个引用类型的列(字段),并为其指定一个字段名称,例如“ Table1 Reference”。编辑器将为您生成一个类似于 table1Reference 的字段键。

现在,如果您有一个来自Table1的记录要链接到Table2,则可以执行以下操作:

wixData.query('Table1')
.eq('title', 'uniqueTitle')
.find()
.then((results) => {
    if (results.totalCount !== 1) {
        throw Error('We didn't get a record from Table1');
    }
    // Success add this in a new record in Table2
    let table1Item = results.items[0];
    let table2Data = {
        table1Reference:table1Item._id,
        anotherTable2Field:"Some info for table2"
    };
    return wixData.save('Table2', table2Data)
    .then((savedRecord) => {
        // Successful save!
        // Do something here....
    });
})
.catch((error) => {
    console.log(error);
});

祝你好运! 史蒂夫