dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

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

Open ctaylor-nurx opened this issue 3 years ago • 8 comments

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

ctaylor-nurx avatar Jun 21 '21 17:06 ctaylor-nurx

same problem here! any updates?

Aoke87 avatar Dec 20 '21 10:12 Aoke87

This problem is still happening

zestian56 avatar Feb 03 '22 20:02 zestian56

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

dungtran5 avatar May 19 '22 07:05 dungtran5

Also got this issue

TimGels avatar Jun 01 '22 10:06 TimGels

Also facing this issue

federaljules avatar Jun 10 '22 06:06 federaljules

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.

federaljules avatar Jun 10 '22 08:06 federaljules

@federaljules Thanks, this fixed the issue for me.

ernest-heh avatar Jan 17 '24 14:01 ernest-heh