Netflix / falcor

A JavaScript library for efficient data fetching
http://netflix.github.io/falcor
Apache License 2.0
10.49k stars 446 forks source link

Cannot read property $type of undefined at pathValueMerge #936

Closed gijun19 closed 4 years ago

gijun19 commented 6 years ago

I'm getting the error:

TypeError: Cannot read property '$type' of undefined

Here's the code I have that prepares the JSON that I'm returning to the client.

const falcor = require("falcor");
const FalcorRouter = require("falcor-router");
const request = require("request-promise");
const _ = require("lodash");

class ItemsRouter extends FalcorRouter.createClass([
  {
    route: 'items["id", "title"]',
    async get(pathSet) {
      try {
        if (this.headers.authorization === null) {
          throw new Error("Not authorized.");
        }
        const config = {
          uri: "http://localhost:8080/api",
          headers: this.headers
        };
        const response = await request(config);
        const attributes = pathSet[1];
        const items = JSON.parse(response)["content"];
        let itemsById = {};

        // Prepare JSON Graph.
        items.forEach(item => {
          itemsById[item.id] = _.pick(item, attributes);
        });

        const json = {
          itemsById,
          item: items.map(item => {
            return {
              $type: "ref",
              path: ["itemsById", item.id],
              value: ["itemsById", item.id]
            };
          })
        };

        return json;
      } catch (error) {
        throw new Error(error);
      }
    }
  }
]) {
  constructor(headers) {
    super();
    this.headers = headers;
  }
}

module.exports = ItemsRouter;

Is there anything glaringly wrong with this approach that makes this error happen?

Maybe related to this issue? Issue

NickHeiner commented 6 years ago

Are you able to create a full standalone repo where I can reproduce the issue?

jcranendonk commented 6 years ago

@danielparkk Are you still encountering this issue? Which version of Falcor are you using?