我该怎么办?我在 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();
});
});
输出 = ReferenceError:窗口未定义
假设您知道您正在运行哪个操作系统,最可靠的方法是使用它的本机 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}');
`]);
您还可以显示提示,例如是/否等。查看更多信息此处
Update dialog package 似乎可以开箱即用,但看起来没有 PowerShell 提供的选项那么多。
NodeJS 运行在给定操作系统的控制台上,也就是说,window.alert 实际上是一个浏览器 API 命令,你不能使用它,作为替代方案,你可以:
要调用本机对话框,您需要访问操作系统本机库,您可以使用允许您绑定这些库的Node FFI 模块来做到这一点。 或者,为了简单起见,只需使用该函数的现有实现之一,例如:node-native-dialog、mitsobox 或 dialog。
免责声明:我没有开发或无法证明这些的质量 模块,如果确实需要本机对话框,请自行承担使用风险 为了您的开发,实现您自己的操作系统库绑定。
为 Node 和浏览器安装跨平台、同构的警报(以前的警报节点)
npm i alert -g
使用这个库:
var alert = require('alert');
alert('Hello');
我找不到它的记录位置,但我一直在我的
global.alert()
代码中使用 react-native
进行调试。该应用程序由 Appium
控制,所以我无法使用 console.log
。
您也可以在消息框中添加标题和消息正文。
const { spawnSync } = require('child_process');
const title = "My Title";
const message = "Hello, world!";
spawnSync("powershell.exe", ["-Command", `Add-Type -AssemblyName PresentationCore,PresentationFramework; [System.Windows.MessageBox]::Show('${message.replace(/'/g, "''")}', '${title.replace(/'/g, "''")}', 0);`]);
0
方法调用末尾的[System.Windows.MessageBox]::Show()
指定消息框应该有一个确定按钮。