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

relative time plugin throws: asynchronous error TypeError: v is not a function #1545

Open
ctaylor-nurx opened this issue Jun 21, 2021 · 7 comments

Comments

@ctaylor-nurx
Copy link

Describe the bug
Trying to use relative time formatting for relative times less than a minute throws an error:

asynchronous error TypeError: v is not a function
    at Object.n.fromToBase (/Users/christophe.taylor/nurx/deploy/node_modules/dayjs/plugin/relativeTime.js:1:1086)
    at i (/Users/christophe.taylor/nurx/deploy/node_modules/dayjs/plugin/relativeTime.js:1:510)
    at M.n.from (/Users/christophe.taylor/nurx/deploy/node_modules/dayjs/plugin/relativeTime.js:1:1265)
    at Object.exports.app_status_table (/Users/christophe.taylor/nurx/deploy/lib/templates.js:190:23)
    at run (/Users/christophe.taylor/nurx/deploy/bin/build-deploy.js:240:32)

Expected behavior
Expected to be rendered as less than a minute ago

Information
dayjs v1.10.5 (also happened in v1.10.4)

const dayjs = require('dayjs');
const relativeTime = require('dayjs/plugin/relativeTime');

dayjs.extend(relativeTime, {
  thresholds: [
    { l: 's', r: 1 },
    { l: 'ss', r: 59, d: 'second' }, // ? 'ss' format does not exist on locale
    { l: 'm', r: 1 },
    { l: 'mm', r: 120, d: 'minute' },
    { l: 'h', r: 1 },
    { l: 'hh', r: 23, d: 'hour' },
    { l: 'd', r: 1 },
    { l: 'dd', r: 29, d: 'day' },
    { l: 'M', r: 1 },
    { l: 'MM', r: 11, d: 'month' },
    { l: 'y' },
    { l: 'yy', d: 'year' }
  ],
});

  const test_date = new Date().getTime() - 30000;
  console.log(dayjs(new Date(test_date)).fromNow(true)); // -> asynchronous error TypeError: v is not a function
@Aoke87
Copy link

Aoke87 commented Dec 20, 2021

same problem here! any updates?

@zestian56
Copy link

This problem is still happening

@dungtran5
Copy link

I still got this issue, this library should change to new maintainer, original author is too busy for solve new bugs ...

@TimGels
Copy link

TimGels commented Jun 1, 2022

Also got this issue

@federaljules
Copy link

Also facing this issue

@federaljules
Copy link

federaljules commented Jun 10, 2022

Got my issue solved. Not sure if it is related to this issue but it had a similar error message.
In my case it was because I was updating only some of the default locale settings for relativeTime like this:

dayjs.updateLocale('en', {
	relativeTime: {
		d: '%d d',
		dd: '%d d',
		M: '%d mo.',
		MM: '%d mos.',
		y: '%d y',
		yy: '%d y',
	}
});

I figured out that if you don't set ALL of the locale options for the relativeTime it will throw this error (or at least very similar error) when dayjs tries to use one of the relativeTime locale options that you didn't set on updateLocale and doesn't find it.

This is because updateLocale will replace the default relativeTime locale object with the object you set in the updateLocale. So for example in this case it would not have locale options for h, hh, s, ss etc.

The way I fixed this in my use case was to get the default relativeTime locale values and just spread those and update the ones I need to update.

const localeList = dayjs.Ls;

dayjs.updateLocale('en', {
	relativeTime: {
		...localeList['en'].relativeTime,
		d: '%d d',
		dd: '%d d',
		M: '%d mo.',
		MM: '%d mos.',
		y: '%d y',
		yy: '%d y',
	}
});

Hope this helps someone else as well.

@ernest-heh
Copy link

@federaljules Thanks, this fixed the issue for me.

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

7 participants