Currently, I am undertaking the task of replicating Messenger using Next.Js for practice. Throughout this process, I have integrated type definitions and incorporated "Upstash, Serverless access to the Redis database" as part of my project. I meticulously followed the guidelines provided by both NEXT.Js and Upstash Console, leveraging Node.js as directed on the Upstash console. The structure of my typeDefinitions for a Message object is outlined in the following snippet: (typings.d.ts)
export type Message = {
id: string,
message: string,
created_at: number,
username: string,
profilePic: string,
email: string,
};
To facilitate the addition of messages, I implemented an API handling procedure with asynchronous promises. The code snippet below illustrates this implementation:
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next'
import redis from '../../redis';
import { Message } from '../../typings';
type Data = {
message: Message;
}
type ErrorData = {
body: string
}
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Data | ErrorData>
) {
if(req.method != 'POST') {
res.status(405).json({ body: "Method Not Allowed"
});
return;
}
const { message } = req.body;
const newMessage = {
...message,
//Replace the timestamp of the user with the server's timestamp
created_at: Date.now(),
};
// Push to Upstash Redis DB
await redis.hset("messages", message.id, JSON.stringify(newMessage));
res.status(200).json({ message: "newMessage" })
}
The error message indicating 'Type 'string' is not assignable to type 'Message'' and subsequent issues encountered upon saving the code are perplexing. Despite attempting a hard reload, the error persists. Upon interacting with the frontend interface, an additional error emerged: '"A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received"'. I have diligently reviewed the Upstash console but remain unable to resolve these challenges. Any assistance would be greatly appreciated. Thank you.