Node.js 范围问题?

标签 node.js

我正在使用 node.js 制作一个脚本,用于在 i3 中设置我的 dzen2,并且之前没有真正使用过 Node 来完成类似的事情。

我需要从屏幕的几何形状开始,我可以通过以下方式获得:

geometry = getGeo();

function getGeo() {
  var sh = require('child_process').exec("i3-msg -t get_outputs",
    function(error, stdout, stderr) {
      var out = JSON.parse(stdout);
      return out[0].rect; //this is the geometry, {"x":0, "y":0, "width":1280, "height":768}
  });
};

console.log(geometry);

console.log 正在记录未定义的日志。

我不知道正确的方法是什么,我的大脑很累。

最佳答案

您无法从回调函数返回,因为它是异步的。 而是编写另一个函数并将回调对象传递给它。

function getGeo() {
var sh = require('child_process').exec("i3-msg -t get_outputs",
    function(error, stdout, stderr) {
      var out = JSON.parse(stdout);
      getRect(return out[0].rect);
    });
};

function getRect(rect) {
    // Utilize rect here...
}

关于Node.js 范围问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11166266/

相关文章:

node.js - Sequelize 通过关联创建

javascript - 通过express处理jwt token 返回错误 "const token = req.cookies.auth; [0] ^ [0] [0] ReferenceError: req is not defined"

node.js - 无法在 View 目录 NestJs 中查找 View "index"

node.js - 是否可以在 node.js 服务器端使用 google.maps api 库?

node.js - 如何在jade中动态添加多个类名?

javascript - Config.get() 未从文件 : "custom-environment-variables.json" Nodejs, JavaScript 获取配置

node.js - 进程内存不足后返回 Angular API fatal error 处理程序

javascript - 如何在 Node.js 中执行 IIFE 脚本文件

javascript - 将base64转换为图像文件(jpeg、jpg)

node.js - 有什么方法可以自定义 LUIS 中任何话语的分数吗?