Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defining relations results to error when the Mapper recordClass is set to false (POJO) #570

Open
XT-Martinez opened this issue Jun 17, 2020 · 2 comments

Comments

@XT-Martinez
Copy link

Description

I'm implementing a Data Mapper Pattern structure for my project and would like to use POJOs instead of Record instances when dealing with data from a REST API.
However, defining relations results to error when the recordClass in the mapper is set to false (POJO).
I'm not encountering an error if the recordClass is not set.

Environment

  • js-data version: 3.0.8
  • node or browser version: Google Chrome Version 83.0.4103.106
  • operating system: Fedora 32 Silverblue

Steps to reproduce

  1. Use the code below.
import { DataStore } from "js-data";
import { HttpAdapter } from "js-data-http";

const BASE_PATH = "http://localhost:3000/api";
const Store = new DataStore();
const Adapter = new HttpAdapter({ basePath: BASE_PATH });

Store.registerAdapter("http", Adapter, { default: true });

Store.defineMapper("ProductVariation", {
  endpoint: "productVariation",
  schema: {
    type: "object",
    properties: {
      id: { type: "number" },
      productId: { type: "number", indexed: true },
      name: { type: "string" },
    },
  },
  relations: {
    belongsTo: {
      Product: {
        foreignKey: "productId",
        localField: "product",
      },
    },
  },
  recordClass: false,
});

Store.defineMapper("Product", {
  endpoint: "product",
  schema: {
    type: "object",
    properties: {
      id: { type: "number" },
      name: { type: "string" },
      description: { type: "string" },
      isActive: { type: "boolean" },
    },
  },
  relations: {
    hasMany: {
      ProductVariation: {
        foreignKey: "productId",
        localField: "variations",
      },
    },
  },
  recordClass: false,
});
  1. When executed, error encountered
DataStore.js:167 Uncaught TypeError: Cannot convert undefined or null to object
    at Function.getOwnPropertyDescriptor (<anonymous>)
    at DataStore.js:167
@crobinson42
Copy link
Member

I would appreciate some help on this if you have time.

What I have done in my projects is a custom toJSON method because we rely on the Record Schema classes.

@yetrun
Copy link

yetrun commented Dec 11, 2021

@crobinson42 So how to solve it anyway?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants