Thursday, 29 April 2021

TypeError: Illegal Invocation

We're running a Create React App (CRA) web app, to which we've added Google Analytics v4. We initiate analytics using the ga-4-react npm package. Manual initialization in index.js

const ga4react = new GA4React(process.env.REACT_APP_GOOGLE_ANALYTICS_ID);

ga4react.initialize().then((ga4) => {
    ga4.pageview(window.location.path);
}, (err) => {
    Sentry.captureException(err);
});

We're receiving hundreds of errors from the gtag/js file to our Sentry error monitoring platform. This error doesn't really say much to us and we've spent two days trying to find out if anyone's run into a problem like this, but so far we've come up empty. It also doesn't seem to affect user experience, but it's rather annoying for us to monitor.

The error is reported as so to Sentry.

TypeError zq(gtag/js)
Illegal invocation

gtag/js in zq at line 477:453
{snip} ))}}},zq=function(a,b,c){var d=a+"?"+b;c?$c.sendBeacon&&$c.sendBeacon(d,c):pd(d)};var Eq=window,Fq=document,Gq=function(a){var b=Eq._gaUserP {snip}

We also receive as many errors from ga-4-react (the catch-block in the code snippet above). We also tried using the analytics snippet with react-helmet, but had the same result.

This error seems to be generated by a number of browsers (Chrome, Opera, Safari, mobile browsers) on many platforms (Android, Windows 10, OS X) so we can't really pinpoint any specific route, platform, browser that's common with these users.

I also tried to replicate this with AdBlock, blocking trackers, using Do Not Track, but nothing.



from TypeError: Illegal Invocation

How do I update a foreign key (belongsTo) in Sequelize

I have the following model:

'use strict';
const {Model} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Key extends Model {
    static associate(models) {
      Key.belongsTo(models.User, {
        foreignKey: 'userId',
        onDelete: 'CASCADE'
      });
    }
  };
  Key.init({
    keyType: DataTypes.STRING,
    key: DataTypes.JSON
  }, {
    sequelize,
    modelName: 'Key',
  });
  return Key;
};

I then try to create a row, after receiving userId, keyType and key:

...
const Key = KeyModel(sequelize, Sequelize);
const createKey = async (userid, keyType, key) => {
  const result = await Key.create({userId, keyType, key});
  return result;
}

The row gets created successfully in the DB, and i get back an ID (the createdAt and updatedAt are updated as well), but the userId is null.

How should I pass it to the create method so the value gets to the DB? Am I missing something in the model?

PS: the DB is MySQL 8.



from How do I update a foreign key (belongsTo) in Sequelize

Optimize multiple Views to one View - Django

I am using django-autocomplete-light widget for loading data dynamically to display in the HTML forms.

I want to optimize below urls to one url and its views classes to single view as most of the code(90% code) is common across all the views, only few parameters are different.

Here is my urls.py:

path('locations/', views.LocationAutocomplete.as_view(), name='location-autocomplete'),
path('societies/', views.SocietyAutocomplete.as_view(), name='society-autocomplete'),
path('propcats/', views.PropertyCategoryAutocomplete.as_view(), name='propcat-autocomplete'),
...
path('projects/', views.ProjectAutocomplete.as_view(), name='project-autocomplete'),

Here are its Views from views.py:

class LocationAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        if not self.request.user.is_authenticated:
            return Location.objects.none()
        qs = Location.objects.all()
        if self.q:
            qs = qs.filter(name__istartswith=self.q)
        return qs

class SocietyAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        if not self.request.user.is_authenticated:
            return Society.objects.none()
        preferred_area = self.forwarded.get('preferred_area', None)
        qs = Society.objects.filter(location_id__in=preferred_area)
        if self.q:
            qs = qs.filter(name__istartswith=self.q)
        return qs

class PropertyCategoryAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        if not self.request.user.is_authenticated:
            return PropertyCategory.objects.none()
        enquiry_flag = self.request.session.get('enquiry_flag', 3)
        qs = PropertyCategory.objects.filter(type__enq_code = enquiry_flag)
        if self.q:
            qs = qs.filter(name__istartswith=self.q)
        return qs

class FloorAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        if not self.request.user.is_authenticated:
            return Floor.objects.none()
        qs = Floor.objects.all()
        if self.q:
            qs = qs.filter(floor__istartswith=self.q)
        return qs

class ProjectAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        if not self.request.user.is_authenticated:
            return Project.objects.none()
        qs = Project.objects.all()
        if self.q:
            qs = qs.filter(name__istartswith=self.q)
        return qs

I was trying something like this, but not sure how can I pass other relevant parameters such as database query parameters, model name, etc.

class AutoCompleteHandler(autocomplete.Select2QuerySetView):
    
    def __init__(self, model, model_query, *args, **kwargs):
      super().__init__(*args, **kwargs)
      self.model = model
      self.model_query = model_query


    def get_queryset(self):
        if not self.request.user.is_authenticated:
            return self.model.objects.none()
        enquiry_flag = self.request.session.get('enquiry_flag', 1)

        qs = self.model.objects.filter(**self.model_query)

        if self.q:
            qs = qs.filter(name__istartswith=self.q)

        return qs

Can anyone help me to optimize above code?



from Optimize multiple Views to one View - Django