Tag Archives: javascript

JS async await Error: Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules

Uncaught SyntaxError
Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules
await the top level bodies of modules
Can not Run normally

<script type="text/javascript">
// Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules
let x = await tools();
console.log(x);
</script>

After modification

reference resources

<script type="text/javascript">
// demo
(async () => {
    let x = await tools();
    console.log(x);
})();
</script>

Cesium.js:1 Error loading image for billboard: [object Event]

Label pictures are not displayed

Address error

When going to reference local images in the .js file, the path should be introduced in the form of require(), modified as follows

billboard: {
        image: require("../img/boshi.png"),
        pixelOffset: new Cesium.Cartesian2(-120, 0),
        // eyeOffset: new Cesium.Cartesian3(0.0, 0.0, 0.0),
        horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
        verticalOrigin: Cesium.VerticalOrigin.CENTER,
        // scale: 0.25,
        distanceDisplayCondition: new Cesium.DistanceDisplayCondition(100, 20000)
 }

[Solved] MSBUILD : error MSB3428: Could not load the Visual C++ component “VCBuild.exe“

MSBUILD : error MSB3428: Could not load the Visual C++ component “VCBuild.exe”.

This problem occurs in the install front-end project

MSBUILD : error MSB3428: Could not load the Visual C++ component “VCBuild.exe”…

Solution:

1. The administrator opens CMD and sets the agent (optional, if you have one)

set HTTP_PROXY=http://127.0.0.1:1080
set HTTPS_PROXY=http://127.0.0.1:1080

2. Install windows build tool

npm install -g --production windows-build-tools

3. If the card is completely stuck, wait for one minute, and then enter C:\Users\Username\.windows-build-tools

If there is a file in the figure below, the download is successful, and if there is no file, the download fails

If the download fails, download it manually and extract it to C:\Users\Username\

The final status is shown in the figure:

5 Configure system environment variables (Setting -> advanced system settings -> environment variables)

Append to path item

​ C:\Users\13261.windows-build-tools

​ C:\Users\13261.windows-build-tools\python27

6. change the file name

Because the problem I encountered was MSBUILD : error MSB3428: Could not load the Visual C++ component “VCBuild.exe”…

The project will use the executable file VCBuild.exe to build

So I made a copy of vs_BuildTools.exe and modified it to VCBuild.exe, so that cmd can use “VCBuild”!

Because I have already installed python3 or higher, the command is also “python”

so python2.7 I will also modify the executable to python.exe -> python2.exe, pythonw.exe -> python2w.exe

TIP: If your front-end project will be built with commands like “python2” or “python3” instead of “python” to execute python code, then you also need to rename the executable

Translated with www.DeepL.com/Translator (free version)

[Solved] react Error: Can‘t perform a React state update on an unmounted component

Warning: Can’t perform a React state update on an unmounted component. This is a no-op, but it indic

In the development of react, we may often encounter this warning:

Can't perform a React state update on an unmounted component.This is a no-op, but it indicates a memory leak in your application.

We cannot set state after the component is destroyed to prevent memory leakage

As for the above errors when switching routes in react, the actual reason is that after the component is mounted, you perform asynchronous operations, such as Ajax requests or setting timers, and you perform setstate operation in callback. When you switch routes, the component has been unmounted. At this time, the callback is still executing in the asynchronous operation, so the setstate does not get a value.

There are two solutions:

1. Clear all operations when uninstalling (e.g. abort your Ajax request or clear timer)

componentDidMount = () => {
    //1.ajax request
    $.ajax('Your request',{})
        .then(res => {
            this.setState({
                aa:true
            })
        })
        .catch(err => {})
    //2.timer
    timer = setTimeout(() => {
        //dosomething
    },1000)
}
componentWillUnMount = () => {
    //1.ajax request
    $.ajax.abort()
    //2.timer
    clearTimeout(timer)
}

2. Set a flag and reset it when unmount

componentDidMount = () => {
    this._isMounted = true;
    $.ajax('Your Request',{})
        .then(res => {
            if(this._isMounted){
                this.setState({
                    aa:true
                })
            }
        })
        .catch(err => {})
}
componentWillUnMount = () => {
    this._isMounted = false;
}

3. The simplest way (oil of gold)

componentDidMount = () => {
    $.ajax('Your Request',{})
    .then(res => {
        this.setState({
            data: datas,
        });
    })
    .catch(error => {
 
     });
}
componentWillUnmount = () => {
    this.setState = (state,callback)=>{
      return;
    };
}

Vue warn]: vue3 element Component emit Pass Event Error

vue3 in element component emit pass event error
Report error:

Vue warn]: Extraneous non-emits event listeners (cancel, confirm, modelClose) were passed to component but could not be automatically inherited because component renders fragment or text root nodes. If the listener is intended to be a component custom event listener only, declare it using the “emits” option.

Sub-component throwing events

 emits: ['cancel', 'confirm', 'modelClose'],
  setup (props, context) {
    const { showDialog } = toRefs(props)
    const show = computed(() => {
      return showDialog.value
    })
    const cancel = () => {
      context.emit('cancel')
    }
    const confirm = () => {
      context.emit('confirm')
    }
    const modelClose = () => {
      context.emit('modelClose')
    }
    return {
      show, cancel, confirm, modelClose
    }

Parent component receives events

`<user-info :showDialog="showDialog" @cancel="showDialog=false" @confirm="showDialog=false" @modelClose="showDialog=false"></user-info>

<script setup> Error: ‘defineProps‘ is not defined [How to Solve]

Solution 1: in eslintrc. JS env add configuration

  env: {
    'vue/setup-compiler-macros': true // New
  }

An error may be reported when the development service is restarted after configuration:

Environment key “vue/setup-compiler-macros” is unknown

Recompile again and the error disappears.

Solution 2: add global configuration in eslintrc.js

  globals: {
    defineProps: "readonly",
    defineEmits: "readonly"
  }

[Solved] Vue 3 Script Setup ESLint Error: ‘defineProps‘ is not defined

Solution: when using Vue 3 script setup, eslint reports the error ‘defineprops’ is not defined


Vue 3’s script setup syntax introduces compiler macros of defineprops, definemits, defineexpose, withdefaults. However, in some cases, eslint will report an error. The above compiler macro functions are not defined.

This article will introduce two solutions to solve this problem (assuming that your project is initialized with Vue CLI).


Step 1. Check the version of eslint-plugin-Vue

npm list eslint-plugin-vue

If the version is in V8 Above 0.0, jump to step 2, otherwise go directly to the content of step 3.


Step 2. Version is V8.0.0+

Open eslintrc.JS file and modify it as follows:

  env: {
    node: true,
    // The Follow config only works with eslint-plugin-vue v8.0.0+
    "vue/setup-compiler-macros": true,
  },

Step 3. Version is V8 Below 0.0

Open eslintrc.JS file and modify it as follows:

  // The Follow configs works with eslint-plugin-vue v7.x.x
  globals: {
    defineProps: "readonly",
    defineEmits: "readonly",
    defineExpose: "readonly",
    withDefaults: "readonly",
  },

If your version of  eslint-plugin-vue is under V8, I don’t recommended you to upgrade the version,especially you had used a lot of ts dependency.

[Solved] ECharts Console Error: `resize` should not be called during main process

When using ecarts, the console reports an error ` resize ` should not be called during main process


This situation may occur in the scenario where echarts is used in combination with Vue 3, especially after the echarts instance is wrapped with Ref.

This article is excerpted from another blog post, the use of echarts 5 in the development of Vue 3


1. Problem analysis

In general, the encapsulation of ecarts chart does not need to expose the ecarts object to the rendering context. If you do intend to declare an echots object as a response, use shallowref instead of ref:

// GOOD
const chart = shallowRef<echarts.ECharts>(); 

// BAD
const chart = ref<echarts.ECharts>();

If you do not use shallowref, the command line may report an error ‘resize’ should not be called during main process; In fact, any instance created by a third-party library should be processed responsively using shallowref instead of ref.

[Solved] Vue Error: /sockjs-node/info?t=

Error reporting description

After running NPM run serve, the interface has been called in the network: http:// * * */sockjs node/info?t=***

Error reporting reason

Sockjs node is a JavaScript library that provides cross browser JavaScript APIs and creates a low latency, full duplex communication channel between browsers and web servers.

There are related calls in the dependent source code

Solution

CTRL + P open file node_modules\sockjs-client\dist\sockjs.JS
Ctrl + G jump to line 1611
refer to the figure below, comment out the call of line 1611, and restart the project

[Solved] webpack Package Error: TypeError: this.getOptions is not a function style-loader

I used style-loader in the project today, and found an error in packaging: typeerror: this Getoptions is not a function
visual inspection is a version problem. The default version of style-loader is 3.3.1. The reduced version is 2.0.0, and the problem is solved
PS: the webpack version used is 4.28.4