如何在 node.js 中显示警报消息
How can show alert message in node.js
我该怎么办??我在 node.js 上工作。我不能使用 alert("sometext");
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("project");
dbo.collection("Register").findOne({Email: req.body.email} , function(err, result) {
if (result.Password == req.body.psw) {
console.log("Correct go next page");
}
else{
window.alert("sometext");
});
db.close();
});
});
output = ReferenceError: window 未定义
NodeJS运行在给定操作系统的控制台上,也就是说,window.alert实际上是一个浏览器API命令,你不能使用它,作为替代,你可以:
- 使用 console.log 将在 STDOUT 上输出信息(也称为标准输出,也就是仅在控制台上打印)。
- 使用 console.error,这与上面的完全相同,但会将内容通过管道传输到 STDERR,这对于日志记录或进程输出识别目的很有用。
- 将您的 Node 应用程序包装在像 Electron 这样的容器上,老实说,这太过分了。
- 最后,从您的 OS:
调用 本机对话框 API
要调用本机对话框,您需要访问 OS 本机库,您可以使用允许绑定这些库的 Node FFI module 来实现。
或者,为了简单起见,只使用该函数的现有实现之一,例如:node-native-dialog, mitsobox or dialog.
Disclaimer: I did not develop or can attest the quality of those
modules, use at your own risk, if native dialogs are really required
to your development, implement your own binding of the OS libraries.
假设您知道 OS 您在哪个 运行 上,最可靠的方法是使用它的原生 shell(到目前为止,我尝试过的大多数 NPM 包都没有开箱即用)。对于 windows Powershell,它可以是这样的:
const { spawnSync } = require('child_process');
const messag = "Hello world";
spawnSync("powershell.exe", [`
Add-Type -AssemblyName PresentationCore,PresentationFramework;
[System.Windows.MessageBox]::Show('${messag}');
`]);
您还可以显示提示,例如是/否等。查看更多信息here
Update dialog package 似乎开箱即用,但似乎没有 PowerShell 提供的选项那么多。
我找不到它的记录位置,但我一直在我的 react-native
代码中使用 global.alert()
进行调试。该应用程序由 Appium
控制,所以我无法使用 console.log
。
为 Node 和浏览器安装跨平台、同构警报(以前称为 alert-node)
npm i alert -g
使用这个库:
var alert = require('alert');
alert('Hello');
我该怎么办??我在 node.js 上工作。我不能使用 alert("sometext");
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("project");
dbo.collection("Register").findOne({Email: req.body.email} , function(err, result) {
if (result.Password == req.body.psw) {
console.log("Correct go next page");
}
else{
window.alert("sometext");
});
db.close();
});
});
output = ReferenceError: window 未定义
NodeJS运行在给定操作系统的控制台上,也就是说,window.alert实际上是一个浏览器API命令,你不能使用它,作为替代,你可以:
- 使用 console.log 将在 STDOUT 上输出信息(也称为标准输出,也就是仅在控制台上打印)。
- 使用 console.error,这与上面的完全相同,但会将内容通过管道传输到 STDERR,这对于日志记录或进程输出识别目的很有用。
- 将您的 Node 应用程序包装在像 Electron 这样的容器上,老实说,这太过分了。
- 最后,从您的 OS: 调用 本机对话框 API
要调用本机对话框,您需要访问 OS 本机库,您可以使用允许绑定这些库的 Node FFI module 来实现。 或者,为了简单起见,只使用该函数的现有实现之一,例如:node-native-dialog, mitsobox or dialog.
Disclaimer: I did not develop or can attest the quality of those modules, use at your own risk, if native dialogs are really required to your development, implement your own binding of the OS libraries.
假设您知道 OS 您在哪个 运行 上,最可靠的方法是使用它的原生 shell(到目前为止,我尝试过的大多数 NPM 包都没有开箱即用)。对于 windows Powershell,它可以是这样的:
const { spawnSync } = require('child_process');
const messag = "Hello world";
spawnSync("powershell.exe", [`
Add-Type -AssemblyName PresentationCore,PresentationFramework;
[System.Windows.MessageBox]::Show('${messag}');
`]);
您还可以显示提示,例如是/否等。查看更多信息here
Update dialog package 似乎开箱即用,但似乎没有 PowerShell 提供的选项那么多。
我找不到它的记录位置,但我一直在我的 react-native
代码中使用 global.alert()
进行调试。该应用程序由 Appium
控制,所以我无法使用 console.log
。
为 Node 和浏览器安装跨平台、同构警报(以前称为 alert-node)
npm i alert -g
使用这个库:
var alert = require('alert');
alert('Hello');