来自 taskpane.js 的 MySQL 驱动程序模块的空引用 Microsoft Office 加载项 [英] Null reference from MySQL driver module from taskpane.js a Microsoft Office Add-In

查看:26
本文介绍了来自 taskpane.js 的 MySQL 驱动程序模块的空引用 Microsoft Office 加载项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Microsoft Office 加载项访问 MySQL 数据库.我可以让 Add-In 在 MsWord 中工作,但是当我尝试使用 MySQL Node.js 驱动程序时,mysql"无法正常工作.对象在运行时显示为 null.

我在 taskpane.js 的开头添加了这个:

import { mysql } from 'mysql';做了以下事情:npm 安装 mysqlnpm 审计修复npm 安装 @types/mysql将以下内容添加到 webpack.config.js target: 'node',"

然后在代码中,当我运行它时会触发并显示mysql undefined":

 if (!mysql) {context.document.body.insertParagraph(mysql undefined", Word.InsertLocation.end);

我遗漏了什么或做错了什么?

解决方案

这行不通,因为您正尝试在前端使用服务器库 mysql.虽然两者都是用 JavaScript 编写的,但它们所使用的环境却大不相同.

下面的图表非常简洁地解释了它 - 来自

本质上,对于反应库,您使用 node.js 作为 *.jsx 或 *.js 构建助手、打包运行器等. 所以最终结果应该只是一个 javascript 文件.如果您使用的是 create react 应用程序的默认构建,则类似于 main.123456.js.该文件在浏览器上下文中将是有意义的,因为您将在此处拥有 DOM 项和 HTML 特定的内容.

对于 MySQL 库 - 您使用 node.js 作为 HTTP 服务器.这将无法访问 DOM,但因为它作为某个计算单元上的进程运行,它将为您提供不同的功能,使您的 MySQL 连接能够正常工作.

office.js 在浏览器上下文中运行,你基本上是用 react 编写你的应用程序,只是引用一些 office.js API.这意味着下面的行属于您的 React 应用:

context.document.body.insertParagraph("mysql undefined", Word.InsertLocation.end);

不过,您的 MySQL 数据需要来自您的服务器.您可以使用 node.js 作为服务器,这样您就可以使用从 NPM 导入的 MySQL 库.本教程将带您了解如何设置一个非常简单的服务器.

这意味着这一行

import { mysql } from 'mysql';

并且sql连接属于运行在node上的服务器端.

I'm trying to access a MySQL database from a Microsoft Office Add-In. I can get the Add-In to work in MsWord, but when I try to use the MySQL Node.js driver, the "mysql" object shows up as null at runtime.

I added this at the beginning of 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',"

and later in the code, when I run it this fires and shows "mysql undefined":

 if (!mysql) {
  context.document.body.insertParagraph("mysql undefined", Word.InsertLocation.end);

What am I missing or doing wrong?

解决方案

This wouldn't work, since you're trying to use a server library mysql in your front-end. While both are written in javascript, the environments they are working on are very different.

Diagram below explains it pretty neatly - sourced from this website which I'm recommending you to check out.

In essence, for react library, you are using node.js as a *.jsx or *.js build helper, packaging runner, etc.. so end result should be just a javascript file. Something like main.123456.js if you're using create react app's default build. That file will be meaningful in browser context, since you'll have DOM items here, HTML specific stuff.

For MySQL library - you are using node.js as a HTTP server. That won't have access to the DOM, but because it's running as a process on some compute unit, it'll provide you different capabilities, which enable your MySQL connection to work.

office.js operates in browser context, you're basically writing your app with react and just referencing some office.js APIs. That means the line below belongs to your react app:

context.document.body.insertParagraph("mysql undefined", Word.InsertLocation.end);

Your data from MySQL needs to come from your server though. You can use node.js as a server, that way you'll be able to use the MySQL library that you've imported from NPM. This tutorial walks you through how to setup a very simple server.

That means this line

import { mysql } from 'mysql';

and the sql connection belongs to the server side running on node.

这篇关于来自 taskpane.js 的 MySQL 驱动程序模块的空引用 Microsoft Office 加载项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆