prismicio / prismic-client

The official JavaScript + TypeScript client library for Prismic
https://prismic.io/docs/technical-reference/prismicio-client
Apache License 2.0
169 stars 61 forks source link

Unable to encode .... of type object - Prismic.Predicate.at() #77

Closed joZephhh closed 4 years ago

joZephhh commented 5 years ago

Hello,

I've tried to use multiple predicates on a query as the example but when the code (see below) is executed I got an error: Unable to encode no-case-study of type object

const noCasesStudiesProjects = client().then(api => {
    return api
      .query([
        Prismic.Predicates.at('document.type', 'project'),
        Prismic.Predicates.at('document.tags', ['no-case-study'])
      ])
      .then(data => {
        return data.results;
      });
  });

I'm using : Nuxt 2.0.0 and prismic-javascript ^2.0.1

Any ideas ? Thanks

NB : Until yesterday, everything worked well :/ NB2 : Seems to works in build and generate (nuxt) process 🧐

mcrider commented 5 years ago

I'm getting a similar error with any predicate, e.g. with

const articles = await app.$prismic.api.query(Prismic.Predicates.any('document.type', ['articles', 'videos']), { orderings: '[document.first_publication_date]' })

The at predicate works as expected however (await app.$prismic.api.query(Prismic.Predicates.at('document.type', 'articles'), { orderings: '[document.first_publication_date]' }))

(Nuxt 2.4, prismic-javascript 2.0.1)

Full stacktrace:

 ERROR  Unable to encode articles,videos of type object                                                                                                                                                                                                             15:50:48

  at q (node_modules/prismic-javascript/dist/prismic-javascript.min.js:1:10660)
  at Object.any (node_modules/prismic-javascript/dist/prismic-javascript.min.js:1:12120)
  at asyncData (archive.vue:24:0)
  at promisify (.nuxt/utils.js:226:0)
  at Promise.all.Components.map.Component (.nuxt/server.js:195:0)
  at Array.map (<anonymous>)
  at module.exports../.nuxt/server.js.__webpack_exports__.default (.nuxt/server.js:190:0)
  at <anonymous>
  at process._tickDomainCallback (internal/process/next_tick.js:228:7)
mcrider commented 5 years ago

Also unable to run fulltext search queries, getting similar errors. I've tried to build a non-minified prismic-javascript locally so i can debug it, but getting the following error when i run npm run dev.. Any help would be greatly appreciated, really need to get these predicate queries working.

ERROR in ./src/index.ts 7:10
Module parse failed: Unexpected token (7:10)
You may need an appropriate loader to handle this file type.
| import ResolvedApi, { EXPERIMENT_COOKIE, PREVIEW_COOKIE } from './ResolvedApi';
|
> namespace Prismic {
|
|   export const experimentCookie = EXPERIMENT_COOKIE;
 @ multi promise-polyfill/src/polyfill cross-fetch/polyfill ./src/index.ts main[2]
mcrider commented 5 years ago

Okay got a non-minified version of prismic-javascript library so i could debug it, looks like this comes down to this line: https://github.com/prismicio/prismic-javascript/blob/edd2c5b66926492dfabd0371100d9f2bc6b413c4/src/Predicates.ts#L41

Due to some awesome javascript idiosyncrasies, which may be dependent on the version of Node being used, instanceof won't work here. Dropped in some logging, hope you find it as fascinating as I do :P

  console.log(value); // [ 'articles', 'videos' ]
  console.log(typeof value); // object
  console.log(value instanceof Array); // false
  console.log(Array.isArray(value)); // true
  console.log(Object.prototype.toString.call(value)); // [object Array]

Can we switch to use one of the last two methods to test if its an array? Both fix the issue. @arnaudlewis ?

kekkorider commented 5 years ago

Any updates on this? this problem is really annoying :/

typewriter-software commented 5 years ago

@kekkorider I used the patch-package npm package to bypass this bug. Prismic doesn't want to seem to address it. Let me know and I can post the patch I'm using.

funkeeflow commented 5 years ago

any news on this? this is really annoying ...

mcrider commented 5 years ago

Ran into this again on another project, for those still experiencing it here is a patch you can use with the patch-package package: https://gist.github.com/typewriter-software/0fa1e8fa62b4101ceab1ebc421ecb421

atinux commented 5 years ago

Thank you so much @mcrider for finding a workaround.

srenault commented 4 years ago

I close that issue since a PR fixed it.