Error Code: Entity Class extends value undefined is not a constructor or null – “This might be caused by a React Class Component being rendered in a Server Component, React Class Components only works in Client Components. Read more: “
Javascript version: 10.8.1
redis version: 4.6.15
redis-om version: 0.4.6
I am trying to use the redis libraries to streamline JSON use
Here is my schema:
import { Entity, Schema } from 'redis-om';
class Car extends Entity {} // <---------- ERROR HERE *************
let carSchema = new Schema(Car, // Schema is for the Car entity
{
make: { type: 'string' }, // The 'make' field of the car is a string
model: { type: 'string' }, // The 'model' field of the car is a string
image: { type: 'string' }, // The 'image' field of the car is a string
description: { type: 'string' }, // The 'description' field of the car is a string
},
{
dataStructure: 'JSON', // Use JSON as the data structure
}
);
export default carSchema;
The schema is referenced here:
import { client, connect } from '../redis';
import carSchema from './schema';
import { Repository } from 'redis-om';
// Exporting an asynchronous function named 'createCar'
export async function createCar(data) {
try {
await connect();
const repository = new Repository(carSchema, client);
const car = repository.createEntity(data);
const id = await repository.save(car);
return id;
} catch (error) {
console.error('Error creating car:', error);
throw error;
}
}
Here is how I handle my db connection:
import { createClient } from 'redis';
// Create a new client instance
const reconnectDelay = 500; // 500ms
const client = createClient({
url: process.env.REDIS_URL,
});
async function connect() {
if (!client.isOpen) {
await client.connect();
console.log('Connected to Redis');
}
}
And here is where the API is called:
const handleSubmit = async (event) => { // Define an asynchronous function to handle form submission
event.preventDefault(); // Prevent the page from reloading when the form is submitted
// Orginzie json for easy handling with redis
const form = new FormData(event.target); // Create a new FormData object from the form element
const formData = Object.fromEntries(form.entries()); // Convert FormData entries to a plain object
console.log(formData); // Log the form data to the console
const res = await fetch('/api/cars', { // Send a POST request to the '/api/cars' endpoint
body: JSON.stringify(formData), // Convert form data to a JSON string for the request body
headers: {
'Content-Type': 'application/json' // Set the request content type to JSON
},
method: 'POST', // Specify the request method as POST
});
const result = await res.json(); // Parse the JSON response
console.log(result); // Log the result to the console
};
I feel like this has to be a version error, but I am using all of the latest.
Everything is up to date:
- npm install redis-om
- npm install redis
I also downloaded the latest version of javascript
If I don’t use the Entity, I cannot map json blocks correctly, so I am pretty stuck.
I am expecting the json from my form to map into this schema so that I can send it to redis
A Z is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.