英文:
How to fill a reference field automatically in Wix (Velo), with or without code?
问题
我正在使用一个引用字段,其中包含用户的昵称,以在我的主要集合和由Wix默认创建的'public data'集合之间建立连接。我创建了这个引用字段来填充一个重复器,从两个'main'和'public data'集合中获取数据。是否可以在不使用代码的情况下自动填充引用字段?如果不行,那么如何使用'beforeInsert'钩子来通过代码填充'引用字段'?
我尝试在后端通过以下代码来实现,但它不起作用。
import { currentMember } from 'wix-members-backend';
export function Collection1_beforeInsert(item, context) {
currentMember.getMember()
.then((member) => {
const id = member._id;
const fullName = `${member.contactDetails.firstName} ${member.contactDetails.lastName}`;
const nickname = member.profile.nickname;
console.log(nickname);
item.referenceField = "nickname";
// return member;
})
.catch((error) => {
console.error(error);
});
return item;
}
请注意,上述代码似乎尝试将引用字段设置为字符串"nickname",而不是实际昵称值。您可能需要更改这部分代码,以将实际的昵称值分配给引用字段。
英文:
I am using a reference field, which contains users nickname, to make a connection between my main collection and 'public data' collection created as default by Wix. I created this reference field to populate a repeater from the two 'main' and 'public data' collections. Is it possible to automatically fill a reference field without using code? If not, then how can use 'beforeInsert' hook to fill the 'the reference' field using code.
I tried to do so in the backend by this code, but it doesn't work.
import { currentMember } from 'wix-members-backend';
export function Collection1_beforeInsert(item, context) {
currentMember.getMember()
.then((member) => {
const id = member._id;
const fullName = `${member.contactDetails.firstName} ${member.contactDetails.lastName}`;
const nickname = member.profile.nickname
console.log(nickname)
item.referenceField= "nickname"
// return member;
})
.catch((error) => {
console.error(error);
});
return item;
}
答案1
得分: 0
首先,我假设您正在使用普通的引用字段而不是多重引用字段。
其次,在继续之前,了解引用字段的工作方式很重要。引用字段保存了它所引用的项目的ID。因此,当您说引用字段包含用户的昵称时,这是不正确的。它可以包含PrivateMembersData集合中具有所需昵称的项目的ID。
第三点,正如刚才提到的,昵称字段并不存在于PublicData集合中。它属于PrivateMembersData集合。
因此,如果您想要将您的集合与另一个集合连接,使用昵称字段,您需要将您的引用字段设置为引用PrivateMembersData集合,然后将正确的ID存储在该字段中。
(附带说明:在您的代码中,您在每个引用字段值中都放置相同的字符串"nickname"
。您可能想使用不带引号的nickname
。您还没有正确使用Promise。)
您可以尝试以下代码。它应该让您更接近您寻找的内容。
import { currentMember } from 'wix-members-backend';
export async function Collection1_beforeInsert(item, context) {
const member = await currentMember.getMember();
item.referenceField = member._id;
return item;
}
英文:
First off, I'm assuming you're using a regular reference field and not a multi reference field.
Second, it's important to understand how reference fields work before continuing. The reference field holds the ID of the item it is referencing. So when you say the reference field contains a user's nickname, that can't be true. It can contain the ID of the item in PrivateMembersData collection with the nickname you want.
Third, as just mentioned, the nickname field does not exist in the PublicData collection. It is part of the PrivateMembersData collection.
So, if you want to connect your collection to another with the nickname field, you need to set your reference field to reference the PrivateMembersData collection and then store the proper ID in that field.
(Side point: In your code you are putting the same string, "nickname"
, in every reference field value. You probably meant to use nickname
without the quotes. You're also not using promises correctly.)
You can try this code. It should get you closer to what you're looking for.
import { currentMember } from 'wix-members-backend';
export async function Collection1_beforeInsert(item, context) {
const member = await currentMember.getMember();
item.referenceField = member._id;
return item;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论