简体   繁体   English

Next.js 错误:无法读取未定义的属性(读取“调用”),try/catch 不起作用 onClick?

[英]Next.js Error: Cannot read properties of undefined (reading 'call'), try/catch won't work onClick?

var mysql = require("mysql");
import React from "react";

export async function mySqlQuery(myQuery) {
  try {
    console.log("Running query...");

    var connection = mysql.createConnection({
      host: process.env.MYSQL_HOST,
      user: process.env.MYSQL_USERNAME,
      password: process.env.MYSQL_PASSWORD,
      database: process.env.MYSQL_DATABASE,
    });

    connection.connect();

    connection.query(myQuery, function (error, results) {
      if (error) throw error;
      const output = results[0].solution;
      console.log(output);
    });

    connection.end();
  } catch (e) {
    console.error(e.message); // or "throw Error(e.message)", this shows it directly on top of the webpage
  }
}

The button i use to call the function (without the () => Next.js thinks it's an object)我用来调用 function 的按钮(没有() => Next.js 认为它是一个对象)

<button onClick={() => mySqlQuery("SELECT 1 + 190 / 2 AS solution;")}> SQL PUSH </button>

I tried this too我也试过这个

<button onClick={async () => await mySqlQuery("SELECT 1 + 190 / 2 AS solution;") } > SQL PUSH </button>

I get the following error: Uncaught (in promise) Error: Cannot read properties of undefined (reading 'call')我收到以下错误: Uncaught (in promise) Error: Cannot read properties of undefined (reading 'call')

This is the error in the DevTools console:这是 DevTools 控制台中的错误: DevTools 控制台中的错误

This is the error in Next.js, when i use throw Error() .这是 Next.js 中的错误,当我使用throw Error()时。 这是 Next.js 中的错误

The code inside the try block is erroring, and you're seeing the error being caught/thrown in the catch block. try块内的代码出错,您会看到在catch块中捕获/抛出错误。

mysql is a Node.js package, it can't be used on the client-side in the browser. mysql是 Node.js package,不能在浏览器的客户端使用。 You have to move the logic to the backend or to an API route, then make a request against that on the client-side.您必须将逻辑移动到后端或 API 路由,然后在客户端发出请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的运行时错误类型错误:当我尝试使用 next.js 的图像组件时无法读取未定义的属性(读取“调用”) - Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'call') when I try to use the Image component of next.js 类型错误:无法读取 Next.js 上未定义的属性(读取“调用”) - TypeError: Cannot read properties of undefined (reading 'call') on Next.js Next.js 错误:无法读取未定义的属性(读取“集合”) - Next.js Error: Cannot read properties of undefined (reading 'collection') Next.js:无法读取未定义的属性(读取“indexOf”) - Next.js: Cannot read properties of undefined (reading 'indexOf') Next.js | TypeError:无法读取未定义的属性(读取“秘密”) - Next.js | TypeError: Cannot read properties of undefined (reading 'secret') 错误 - 类型错误:使用 next.js 中的 redux 存储时无法读取未定义的属性(读取“getState”) - error - TypeError: Cannot read properties of undefined (reading 'getState') while using redux store in next.js Mongoose 和 Next.js:未处理的运行时错误 TypeError:无法读取未定义的属性(读取“令牌”) - Mongoose and Next.js: Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'Token') 类型错误:无法读取未定义的属性(读取“原型”)。 Next.Js 和 Express - TypeError: Cannot read properties of undefined (reading 'prototype'). Next.Js and Express Next.js &amp; Firebase -&gt; TypeError:无法读取未定义的属性(读取“应用程序”) - Next.js & Firebase -> TypeError: Cannot read properties of undefined (reading 'apps') TypeError:无法读取 Next.js 中 null 的属性(读取“默认”) - TypeError: Cannot read properties of null (reading 'default') in Next.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM