簡體   English   中英

試圖讓我的函數能夠擁有動態選擇器,這樣我就不必重復自己了

[英]Trying to make my functions be able to have dynamic selectors so i dont have to repeat myself

這是我的代碼,我發現自己不得不為解析 json 或制作函數做很多事情。

// Adder Functions... hate repeating myself
async function addPetImagesToPet(petId, fileName) {
  const pet = await db.Pet.findById(petId);
  await pet.petImages.push({'fileName':fileName});
  pet.updated = Date.now();
  await pet.save();
  //console.log(pet);
}

async function addPropertyImagesToProperty(propertyId, fileName) {
  const property = await db.Property.findById(propertyId);
  await property.propertyImages.push({'fileName':fileName});
  property.updated = Date.now();
  await property.save();
  //console.log(property);
}

async function addAccountImagesToAccount(accountId, fileName) {
  const account = await db.Account.findById(accountId);
  await account.accountImages.push({'fileName':fileName});
  account.updated = Date.now();
  await account.save();
  //console.log(account);
}

//how can I do `await db.${my_type/Object_Name!!! <--this is what im asking about}.findById(this obviously can be a var np)

我發現自己在我的大部分服務中重復了很多次,我試圖在我的大部分服務前端和后端進一步擴展它。 如果我知道如何做到這一點,我實際上可以為每個 object 類型的大多數用例提供一個“CRUD”服務。

來了很多,一直讓我瘋狂解析 JSON 對象,使 object 上的選擇器是動態的......

我使用 ${} 因為這就是我動態構建字符串的方式,但不能這樣做或使其適用於函數或命名,例如,我不能創建一個字符串變量並將該名稱用作 function 或其方法的名稱,但是這正是我想要做的。 再次感謝任何能夠提供幫助的人。

提前致謝。

您可以在此處執行類似的操作,使用各個值創建 hashMap。

const hashMap = {
  'pet': {
    dbName:"Pet",
    key: 'petImages'
  },
  'account': {
    dbName: "Account",
    key: 'accountImages'
  },
  'property':{
    dbName: "Property",
    key: 'propertyImages'
  }
}

async function addImagesToAccount(accountId, fileName, type) { // type will be the key for the object above.
  const {dbName, key} = hashMap[type];
  const account = await db[dbName].findById(accountId);
  await account[key].push({'fileName':fileName});
  account.updated = Date.now();
  await account.save();
}

你可以使用這樣的東西:

async function addImages(type, id, fileName) {
  const entity = await db[type].findById(id);

  entity.updated = Date.now();

  const attribute = `${type.toLowerCase()}Images`;
  entity[attribute].push({ fileName })

  await entity.save();
}

async function main() {
  await addImages('Property', 2, 'text-02');
  await addImages('Account', 3, 'text-03');
  await addImages('Pet', 3, 'text-03');
}

另外我認為Factory Pattern可以幫助您編寫可重用的代碼:)。

理想情況下,如果您想使用更多變量,則可能是使用和數組。 保持一個不超過三個變量的函數是一種很好的做法。 但這是個人喜好。

好吧,我會用一行來做到這一點,無需將其包裝在 function

await db.Pet.updateOne({ _id: petId }, { $push: : { petImages : {'fileName':fileName}}, updated: Date.now() });

謝謝大家的評論和幫助。

我的最終代碼看起來像這樣,當然我可以讓它更短。

async function addImagesToObject(objId, objType, fileName) {
  const entity = await db[`${objType}`].findById(objId);
  const attribute = `${objType.toLowerCase()}Images`; //<-- defining selector
  await entity[attribute].push({'fileName':fileName});
  entity.updated = Date.now();
  await entity.save();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM