Objection.js: i use knex with objectionjs but i can't retrieved data error this "TypeError: Cannot read property 'isPartial' of null"

Created on 12 Sep 2019  路  7Comments  路  Source: Vincit/objection.js

hello i use knex with objectionjs but i can't retrieved data
error this "TypeError: Cannot read property 'isPartial' of null" i make relation in database completed

i use mysql database

Model:
Categorie: https://1drv.ms/u/s!An-gKc2bfnTOgYgu0MpqJNbT-nCbgg?e=BoI2S9
Blog: https://1drv.ms/u/s!An-gKc2bfnTOgYgtCw_FvlmwCOSkGg?e=QrmVIz
Controller:
BlogController: https://1drv.ms/u/s!An-gKc2bfnTOgYgvgtEx6uemT4SUbw?e=0IiVoP
thank you advance.

Most helpful comment

You have this

  await Blog
    //.query()
    .relatedQuery("categorie")
    .then(data => {
      res.send(helper.sendResponse(data, "Blog retrieved successfully."));
    })
    .catch(function(err) {
      res.send(helper.sendError("Blog can't retrieved. " + err));
    });

what are you trying to do here? You don't specify a blog id anywhere, so how would objection know for which blog to fetch the categorie? Or are you trying to fetch all Blog instances and load the categorie with them? In that case, you need to use eager:

  await Blog
    .query()
    .eager("categorie")
    .then(data => {
      res.send(helper.sendResponse(data, "Blog retrieved successfully."));
    })
    .catch(function(err) {
      res.send(helper.sendError("Blog can't retrieved. " + err));
    });

The static relatedQuery can only be used for subqueries. The instance method $relatedQuery can be used to build queries to a single relation of a model instance. You probably need to take a closer look to the documentation. The guide section is a good place to start.

All 7 comments

There are some extra parentheses in your models. Indent them correctly and you'll see them.

i recheck source code in model many time but i can't see line extra parentheses at you mean please you can focus problem line thank you.

Line 34 in categorie.js

Oh, actually it's not an extra. You just have the indentations all messed up in that file.

If that's not the cause, then I don't know what is. You didn't provide that much code. If you want me to look into this further, you need to provide a full reproduction I can debug by running a simple command (or two).

Oh, actually it's not an extra. You just have the indentations all messed up in that file.

If that's not the cause, then I don't know what is. You didn't provide that much code. If you want me to look into this further, you need to provide a full reproduction I can debug by running a simple command (or two).

ok this link https://github.com/maprangsoft/maprangsoft-api
database file at root: database.sql
can config database at .env
run
npm install
npm start
this route my problem can't use relation
http://localhost:4000/api/blog
thank you advance. :D

You have this

  await Blog
    //.query()
    .relatedQuery("categorie")
    .then(data => {
      res.send(helper.sendResponse(data, "Blog retrieved successfully."));
    })
    .catch(function(err) {
      res.send(helper.sendError("Blog can't retrieved. " + err));
    });

what are you trying to do here? You don't specify a blog id anywhere, so how would objection know for which blog to fetch the categorie? Or are you trying to fetch all Blog instances and load the categorie with them? In that case, you need to use eager:

  await Blog
    .query()
    .eager("categorie")
    .then(data => {
      res.send(helper.sendResponse(data, "Blog retrieved successfully."));
    })
    .catch(function(err) {
      res.send(helper.sendError("Blog can't retrieved. " + err));
    });

The static relatedQuery can only be used for subqueries. The instance method $relatedQuery can be used to build queries to a single relation of a model instance. You probably need to take a closer look to the documentation. The guide section is a good place to start.

oh thank you very much this is thing i want. ^___^

Was this page helpful?
0 / 5 - 0 ratings