MySQL 来自 taskpane.js Microsoft Office 加载项的驱动程序模块的空引用
Null reference from MySQL driver module from taskpane.js a Microsoft Office Add-In
我正在尝试从 Microsoft Office 加载项访问 MySQL 数据库。我可以让加载项在 MsWord 中工作,但是当我尝试使用 MySQL Node.js 驱动程序时,“mysql”对象在 运行 处显示为空时间.
我在 taskpane.js 的开头添加了这个:
import { mysql } from 'mysql';
did the following:
npm install mysql
npm audit fix
npm install @types/mysql
added the following to webpack.config.js "target: 'node',"
稍后在代码中,当我 运行 它会触发并显示“mysql undefined”:
if (!mysql) {
context.document.body.insertParagraph("mysql undefined", Word.InsertLocation.end);
我错过了什么或做错了什么?
您需要先声明变量
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "username",
password: "password",
database: 'satavasename',
port: 3306,
ssl: true
});
这是行不通的,因为您正试图在 front-end 中使用服务器库 mysql
。虽然两者都是用 javascript 编写的,但它们的工作环境却大不相同。
下图非常清楚地解释了它 - 来自 this website,我建议您查看。
本质上,对于 React 库,您正在使用 node.js 作为 *.jsx 或 *.js build helper,packaging runner 等。 所以结束结果应该只是一个 javascript 文件。如果您使用的是 create react 应用程序的默认构建,则类似于 main.123456.js
。该文件将在浏览器上下文中有意义,因为您将在此处有 DOM 个项目,HTML 个特定内容。
对于 MySQL 库 - 您正在使用 node.js 作为 HTTP 服务器。它无法访问 DOM,但因为它是 运行 作为某些计算单元上的进程,它会为您提供不同的功能,使您的 MySQL 连接能够正常工作。
office.js
在浏览器上下文中运行,你基本上是在用 React 编写你的应用程序并且只是引用一些 office.js API。
这意味着下面的行属于你的反应应用程序:
context.document.body.insertParagraph("mysql undefined", Word.InsertLocation.end);
您来自 MySQL 的数据需要来自您的服务器。您可以使用 node.js 作为服务器,这样您就可以使用从 NPM 导入的 MySQL 库。 This tutorial walks you through how to setup a very simple server.
这意味着这一行
import { mysql } from 'mysql';
并且 sql 连接属于节点上的服务器端 运行。
我正在尝试从 Microsoft Office 加载项访问 MySQL 数据库。我可以让加载项在 MsWord 中工作,但是当我尝试使用 MySQL Node.js 驱动程序时,“mysql”对象在 运行 处显示为空时间.
我在 taskpane.js 的开头添加了这个:
import { mysql } from 'mysql';
did the following:
npm install mysql
npm audit fix
npm install @types/mysql
added the following to webpack.config.js "target: 'node',"
稍后在代码中,当我 运行 它会触发并显示“mysql undefined”:
if (!mysql) {
context.document.body.insertParagraph("mysql undefined", Word.InsertLocation.end);
我错过了什么或做错了什么?
您需要先声明变量
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "username",
password: "password",
database: 'satavasename',
port: 3306,
ssl: true
});
这是行不通的,因为您正试图在 front-end 中使用服务器库 mysql
。虽然两者都是用 javascript 编写的,但它们的工作环境却大不相同。
下图非常清楚地解释了它 - 来自 this website,我建议您查看。
本质上,对于 React 库,您正在使用 node.js 作为 *.jsx 或 *.js build helper,packaging runner 等。 所以结束结果应该只是一个 javascript 文件。如果您使用的是 create react 应用程序的默认构建,则类似于 main.123456.js
。该文件将在浏览器上下文中有意义,因为您将在此处有 DOM 个项目,HTML 个特定内容。
对于 MySQL 库 - 您正在使用 node.js 作为 HTTP 服务器。它无法访问 DOM,但因为它是 运行 作为某些计算单元上的进程,它会为您提供不同的功能,使您的 MySQL 连接能够正常工作。
office.js
在浏览器上下文中运行,你基本上是在用 React 编写你的应用程序并且只是引用一些 office.js API。
这意味着下面的行属于你的反应应用程序:
context.document.body.insertParagraph("mysql undefined", Word.InsertLocation.end);
您来自 MySQL 的数据需要来自您的服务器。您可以使用 node.js 作为服务器,这样您就可以使用从 NPM 导入的 MySQL 库。 This tutorial walks you through how to setup a very simple server.
这意味着这一行
import { mysql } from 'mysql';
并且 sql 连接属于节点上的服务器端 运行。