我正在开发一个 React Native 应用程序并利用 Appwrite 进行存储和 API 调用。
为了保存喜欢帖子的用户的用户 ID,我使用以下 API 调用:
export async function likeVideoPost(postId, userId) {
try {
const posts = await databases.listDocuments(
appwriteConfig.databaseId,
appwriteConfig.videoCollectionId,
[Query.equal("$id", postId)]
);
if (!posts) throw new Error("Something went wrong [post]");
// Extracting the document from the JSON object
const postDocument = posts.documents[0];
// Extracting the existing likebyusers array from the post document
const existingLikeByUsers = postDocument.likebyusers;
const result = await getCurrentUser();
const updatedLikeByUsers = [...existingLikeByUsers, result];
const updatedPost = await databases.updateDocument(
appwriteConfig.databaseId,
appwriteConfig.videoCollectionId,
[postId],
{
likebyusers: updatedLikeByUsers,
}
);
return updatedPost;
} catch (error) {
throw new Error(error);
}
}
这是我收到的回复:
{
"$collectionId": "xxx",
"$createdAt": "2024-04-22T16:03:18.362+00:00",
"$databaseId": "xxx",
"$id": "xxx",
"$permissions": [
"read(\"user:xxx\")",
"update(\"user:xxx\")",
"delete(\"user:xxx\")"
],
"$updatedAt": "2024-04-22T17:40:05.047+00:00",
"creator": {
"$collectionId": "xxx",
"$createdAt": "2024-04-22T16:02:44.297+00:00",
"$databaseId": "xxx",
"$id": "66268a2447ea7ce4f6b3",
"$permissions": [],
"$updatedAt": "2024-04-22T16:02:44.297+00:00",
"accountId": "xxx",
"avatar": "https://cloud.appwrite.io/v1/avatars/initials?name=Test&project=xxx",
"email": "xxx",
"password": "xxx",
"username": "Test",
"videos": [[Object], [Object]]
},
"likebyusers": [
{
"$collectionId": "xxx",
"$createdAt": "2024-04-22T16:02:44.297+00:00",
"$databaseId": "xxx",
"$id": "xxx",
"$permissions": [Array],
"$updatedAt": "2024-04-22T16:02:44.297+00:00",
"accountId": "xxx",
"avatar": "https://cloud.appwrite.io/v1/avatars/initials?name=Test&project=xxx",
"email": "xxx",
"password": "xxx",
"username": "Test"
},
{
"$collectionId": "xxx",
"$createdAt": "2024-04-22T16:42:10.526+00:00",
"$databaseId": "xxx",
"$id": "xxx",
"$permissions": [Array],
"$updatedAt": "2024-04-22T16:45:42.462+00:00",
"accountId": "xxx",
"avatar": "https://cloud.appwrite.io/v1/avatars/initials?name=Test2&project=xxx",
"email": "xxx",
"password": "xxx",
"username": "Test2"
}
],
"prompt": "Abcprompt",
"thumbnail": "https://cloud.appwrite.io/v1/storage/buckets/6623dbe60f9b23bfe619/files/66268a4511a67cbf7772/preview?width=2000&height=2000&gravity=top&quality=100&project=xxx",
"title": "Abc",
"video": "https://cloud.appwrite.io/v1/storage/buckets/6623dbe60f9b23bfe619/files/66268a45619cf6629001/view?project=xxx"
}
要检索用户喜欢的帖子,我使用以下查询:
// Get video posts Liked by user
export async function getUserLikedPosts(userId) {
try {
const posts = await databases.listDocuments(
appwriteConfig.databaseId,
appwriteConfig.videoCollectionId,
[Query.equal("likebyusers", [userId])]
);
return posts.documents;
} catch (error) {
console.log(error);
throw new Error(error);
}
}
但是,我在控制台中遇到以下错误:AppwriteException:无效查询:无法查询虚拟关系属性。
有人可以帮助我理解并解决这个问题吗?任何见解将不胜感激。
集合结构:我的项目的数据模型
我为我的项目精心设计了一个集合结构,并且非常感谢您的反馈,以确保它符合最佳实践和效率标准。下面,我展示了我制作的结构:
用户收藏:
钥匙 | 类型 | 默认值 |
---|---|---|
用户名 | 字符串 | - |
电子邮件 | 电子邮件 | - |
密码 | 字符串 | - |
头像 | 网址 | - |
账户ID | 字符串 | - |
视频 | 与视频的关系 | - |
说明:
视频收藏:
钥匙 | 类型 | 默认值 |
---|---|---|
标题 | 字符串 | - |
提示 | 字符串 | - |
视频 | 网址 | - |
缩略图 | 网址 | - |
创作者 | 与创作者的关系 | - |
被用户点赞 | 与likebyusers的关系 | - |
说明:
使用的软件版本
我目前在我的项目中使用以下版本:
react-native-appwrite:^0.2.2 开发依赖: @babel/核心:^7.24.3 顺风CSS:3.3.2 世博会:〜50.0.14
我热切期待有关此结构的任何见解、建议或批评。预先感谢您的宝贵意见!
出于好奇,您在构建此项目时是否遵循视频教程?或者这是你自己的事?如果它也是教程的一部分,它可以帮助我理解问题出在哪里。
我认为您的问题在于:
将用户保存在
likebyusers
数组中时,您正在保存用户对象,我不建议这样做。相反,请尝试仅保存用户 ID。
const existingLikeByUsers = postDocument.likebyusers;
const result = await getCurrentUser();
//const updatedLikeByUsers = [...existingLikeByUsers, result];
const updatedLikeByUsers = [...existingLikeByUsers, result.$id];
从这里开始,您尝试查找特定用户喜欢的所有帖子,但是,您使用字符串 ID 来与整个对象进行比较,这是行不通的。
const posts = await databases.listDocuments(
appwriteConfig.databaseId,
appwriteConfig.videoCollectionId,
[Query.equal("likebyusers", [userId])]
);
这是我推荐的。
首先,仅将用户 id 保存在数组中:
const result = await getCurrentUser();
const updatedLikeByUsers = [...existingLikeByUsers, result.$id];
然后,在执行查询时,您可以使用新的
contains
运算符来执行此操作。 equal
运算符应该仍然可以在这里工作,因此您可以尝试两者:
const posts = await databases.listDocuments(
appwriteConfig.databaseId,
appwriteConfig.videoCollectionId,
[Query.contains("likebyusers", [userId])]
);