我如何使用Javascript添加一个新朋友到Solid Inrupt pod(没有Node.js)



我想从UI直接使用JavaScript添加一个新朋友。主要的事情是,在我的固体入侵pod我有一个属性叫知道。我尝试使用这段代码,但不工作:

//Function that adds a new friend to the user's pod
async function addNewFriend(webId, session, friendWebId) {
// Get the Solid dataset of the profile
const profileDataset = await solid.getSolidDataset(webId);
const thing = solid.getThing(profileDataset, webId);
// Get all the Things (resources) in the dataset that have the "knows" property
solid.addIri(thing, FOAF.knows, friendWebId);
solid.setThing(profileDataset, thing);
const updatedDataset = await solid.saveSolidDatasetAt(webId, profileDataset, {
fetch: session.fetch,
});
console.log("works!");
}

在一个实心的pod属性中知道你有这样的东西:

foaf:知道c0:me, c1:me, c:me;

指的是:

@prefix c: https://uo281997.inrupt.net/profile/card#.

@prefix c0: https://arias.inrupt.net/profile/card#.

@prefix c1: https://uo277938.inrupt.net/profile/card#.

所以我真的不知道如何更新配置文件的数据集来链接新朋友的新webId。

我假设您使用的是@inrupt/solid-client。有一件重要的事情要知道,它的功能不会改变数据;相反,if返回带有更新数据的新对象。

所以当你调用例如addIrisetThing时,你必须使用返回值。例如:

//Function that adds a new friend to the user's profile
async function addNewFriend(webId, session, friendWebId) {
// Get the Solid dataset of the profile
const profileDataset = await solid.getSolidDataset(webId);
const thing = solid.getThing(profileDataset, webId);
const updatedThing = solid.addIri(thing, FOAF.knows, friendWebId);
const updatedProfileDataset = solid.setThing(profileDataset, updatedThing);
const storedProfileDataset = await solid.saveSolidDatasetAt(webId, updatedProfileDataset, {
fetch: session.fetch,
});
}

还请注意,如果WebID在pod.inrupt.com上,inrupt.com会阻止你的应用程序写入WebID。不过,上述内容应该适用于例如https://solidcommunity.net或https://solidweb.me pod。

最新更新