小编The*_* xd的帖子

Electron回复错误:无法克隆对象

我试图要求主应用程序找到某种设备,我一直在尝试使用 ipc 来做到这一点,但无法使其与异步发送和同步发送一起工作。我怀疑主要是在试图回应诺言。

渲染器:

const recognizedDevices = ipcRenderer.sendSync('findDevice');
console.log(recognizedDevices);
Run Code Online (Sandbox Code Playgroud)

主要的:

ipcMain.on('findDevice', (event) => findDevice(event));
const findDevice = async (event) => {
  let recognizedDevices = await findConnectedDevices();
  if(recognizedDevices){
  console.log("found");
  console.log(recognizedDevices);
  return event.returnValue = recognizedDevices;
  }
//TODO: If no device found
}
Run Code Online (Sandbox Code Playgroud)

主要结果:

    found
[
  HID {
    _events: [Object: null prototype] { newListener: [Function (anonymous)] },
    _eventsCount: 1,
    _maxListeners: undefined,
    _raw: HID {},
    write: [Function: bound write],
    getFeatureReport: [Function: bound getFeatureReport],
    sendFeatureReport: [Function: bound sendFeatureReport],
    setNonBlocking: [Function: bound setNonBlocking],
    readSync: [Function: bound …
Run Code Online (Sandbox Code Playgroud)

ipc electron

3
推荐指数
1
解决办法
4917
查看次数

如何设置 mongoose 等待 mongodb 连接的最长时间

如果数据库在 3 秒后无法连接,我会尝试向客户端发回错误。我从猫鼬文档中发现了这些方法。

1)

  mongoose.connect("mongodb://localhost/smslist?connectTimeoutMS=1000"
Run Code Online (Sandbox Code Playgroud)

2)设置此选项:

connectTimeoutMS: 1000
Run Code Online (Sandbox Code Playgroud)

我已经关闭了服务器以确保它不会响应,我预计客户端会在一秒后收到错误,但不幸的是这两个都不起作用,并且客户端仅在默认情况下 30 秒后收到错误据我所知,对于节点套接字。您知道我该如何解决这个问题吗?

timeout mongoose mongodb node.js

2
推荐指数
1
解决办法
938
查看次数

等待地图结束后再继续

  let shouldHavePaid = 0;

  demographicsArray.map((country) => {
    if (country.checked == true) {
      Price.findOne({ country: country._id }).then((priceRes) => {
        if (priceRes) {
          shouldHavePaid = shouldHavePaid + priceRes.priceSMS * country.count;
        } else {
          shouldHavePaid = shouldHavePaid + 0.1 * country.count; //Default price for unlisted countries
        }
      });
    }
  });

  console.log(`Finish: ${shouldHavePaid}`);
Run Code Online (Sandbox Code Playgroud)

我希望最后的 console.log 在地图之后执行,但它在地图完成之前触发。我期待这个输出,因为据我所知地图应该是同步的而不是异步的。我相信对数据库的请求搞砸了?你在这里有什么建议?

javascript asynchronous node.js async-await

1
推荐指数
1
解决办法
3259
查看次数