upstash / docs

Upstash documentation
https://upstash.com/docs/
9 stars 36 forks source link

xx boolean option does not update the element which is already stored #70

Closed Shivamrai15 closed 6 months ago

Shivamrai15 commented 6 months ago

Path: /oss/sdks/ts/redis/commands/zset/zadd

import { NextResponse } from "next/server"; import { authOptions } from "@/lib/auth"; import { getServerSession } from "next-auth"; import { db } from "@/lib/db"; import { getCurrentUserId } from "@/helpers/getCurrentUserId";

export async function PATCH ( request ) { try { const {conversationId, data} = await request.json();

    const session = await getServerSession(authOptions);

    if (!session){
        return new NextResponse("Unauthorized action", { status : 401 })
    }

    const userId = await getCurrentUserId(session.user.email);

    if (userId !== data.senderId){
        return new NextResponse("Oops! Looks like you can only delete your own messages here.", { status : 401 })
    }

    const deletedMessage = {
        ...data, text : "⊘ This message was deleted"
    }

    const res = await db.zadd(`chat:${conversationId}:messages`,
        { 
            xx: true,
            ch :true
        },
        {
            score : data.timestamp,
            member : JSON.stringify(deletedMessage)
        }
    );

    console.log(res);

    return NextResponse.json({
        success : true,
        message : "Message has been deleted"
    });

} catch (error) {
    console.error(error);
    return new NextResponse("Internal server error", {status : 500});
}

}

In the above code i want to update a message with "Message has been deleted" but it was not updating the message

ytkimirti commented 6 months ago

There can be multiple members with the same score.

If a specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering. https://redis.io/commands/zadd/