0

我目前正在使用 JsForce + Express,但我发现当我尝试在后端操作数据时,API 调用非常慢。具体来说,当我深入了解每个单独对象的 ID/名称时,它会显着减慢/无法加载。

如果我使用以下代码,它会在不到一秒钟的时间内加载:

router.get("/testChain", (req, res) => {
  conn
    .query(
      "SELECT Id, Name, (SELECT Id, Part_Type__c  FROM Monster_Parts__r) FROM Monster_Frame__c"
    )
    .then((result) => {
      let gMonster = {};
      let frames = result.records;
      // Select Frame
      let sFrameNo = randomNumber(frames.length);
      let sFrame = frames[sFrameNo];
      gMonster["Monster_Frame__c"] = sFrame.Id;
      // Break into parts
      let parts = sFrame.Monster_Parts__r.records;
      let sortedParts = parts.reduce((r, o) => {
        var k = o.Part_Type__c;
        if (r[k] || (r[k] = [])) r[k].push(o);
        return r;
      }, {});
      // Select Part
      Object.keys(sortedParts).forEach((key, i, arr) => {
        let parts = sortedParts[key];
        let partNo = randomNumber(parts.length);
        let selectedPart = parts[partNo];
        gMonster[key] = selectedPart;
      });

      // Generate Name
      gMonster["Name"] = sFrame.Name + "-" + randomNumber();
      return gMonster;
    })
    .then((result) => {
      res.json(result);
    });
});

并给我以下输出:

{
"Monster_Frame__c": "a025j000004GsdXAAS",
"Tail__c": {
"attributes": {
"type": "Monster_Parts__c",
"url": "/services/data/v42.0/sobjects/Monster_Parts__c/a015j00000CfGUXAA3"
},
"Id": "a015j00000CfGUXAA3",
"Part_Type__c": "Tail__c"
},
"Leg__c": {
"attributes": {
"type": "Monster_Parts__c",
"url": "/services/data/v42.0/sobjects/Monster_Parts__c/a015j00000CfGUeAAN"
},
"Id": "a015j00000CfGUeAAN",
"Part_Type__c": "Leg__c"
},
"Head__c": {
"attributes": {
"type": "Monster_Parts__c",
"url": "/services/data/v42.0/sobjects/Monster_Parts__c/a015j00000CfGU0AAN"
},
"Id": "a015j00000CfGU0AAN",
"Part_Type__c": "Head__c"
},
"Body__c": {
"attributes": {
"type": "Monster_Parts__c",
"url": "/services/data/v42.0/sobjects/Monster_Parts__c/a015j00000CfGUKAA3"
},
"Id": "a015j00000CfGUKAA3",
"Part_Type__c": "Body__c"
},
"Back__c": {
"attributes": {
"type": "Monster_Parts__c",
"url": "/services/data/v42.0/sobjects/Monster_Parts__c/a015j00000CfGUPAA3"
},
"Id": "a015j00000CfGUPAA3",
"Part_Type__c": "Back__c"
},
"Name": "Serpent-7210"
}

但是,如果我将此行更改为:

  gMonster[key] = selectedPart;

填充特定的 ID(而不是所有字段):

  gMonster[key] = selectedPart.Id;

API 调用有 60% 的时间失败。(有时它会返回仅携带 Id 的所需输出。

有谁知道如何解决这一问题?

4

0 回答 0