簡體   English   中英

異步/等待未分配的對象

[英]Async/Await to an Object Not Assigning

我有一個未分配給對象的 async/await 函數,我不知道為什么。 我正在安慰結果,它似乎是猶太潔食,但是當它達到對對象的實際分配時,它沒有分配。 我將在下面解釋:

所以這里是代碼:

這只是 asyncForEach 的輔助函數:

  async function asyncForEach(array, callback) {
    for (let index = 0; index < array.length; index++) {
      await callback(array[index], index, array);
    }
  }

然后我有以下內容:

const asyncFunc = async () => {
  await asyncForEach(tempPosts, async (tempPost) => {
    if (tempPost.fileName!=''){

      console.log('tempPosts[tempPosts.indexOf(tempPost)]: ',
      tempPosts[tempPosts.indexOf(tempPost)])

      console.log("await fsPromise.readFile(__dirname+'/../picFolder/sharp/'+tempPost.fileName)", 
      await fsPromise.readFile(__dirname+'/../picFolder/sharp                
      /'+tempPost.fileName))

      tempPosts[tempPosts.indexOf(tempPost)]['data'] = 
      await fsPromise.readFile(__dirname+'/../picFolder/sharp/'+tempPost.fileName)

      console.log('after assignment and value of tempPosts in asyncForEach: ', tempPosts)
    }
  })
}

所以這里是三個javascript日志的結果:

console.log('tempPosts[tempPosts.indexOf(tempPost)]: ',
tempPosts[tempPosts.indexOf(tempPost)])

結果是

tempPosts[tempPosts.indexOf(tempPost)]:  { flags: 0,
  fileName: '1552601360288&&travelmodal.png',
  comments: [],
  _id: 5c8ad110ef45f6e51a323a18,
  body: 'asdasdfasdf',
  created: 2019-03-14T22:09:20.427Z,
  __v: 0 }

這似乎是正確的。

console.log("await fsPromise.readFile(__dirname+'/../picFolder/sharp/'+tempPost.fileName)", 
await fsPromise.readFile(__dirname+'/../picFolder/sharp                
  /'+tempPost.fileName))

給...

await fsPromise.readFile(__dirname+'/../picFolder/sharp/'+tempPost.fileName)
<Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 c8 00 00 00 62 08 06 00 00 00 15 df 9c 16 00 00 00 09 70 48 59 73 00 00 16 25 00 00 16 25 01 ... >

這是我想要的真正長的數據緩沖區字符串。 涼爽的。

然而

after assignment and value of tempPosts in asyncForEach:  [ { flags: 0,
    fileName: '1552601360288&&travelmodal.png',
    comments: [],
    _id: 5c8ad110ef45f6e51a323a18,
    body: 'asdasdfasdf',
    created: 2019-03-14T22:09:20.427Z,
    __v: 0 },
  { flags: 0,
    fileName: '1552601320137&&Screen Shot 2019-03-09 at 10.03.09 AM.png',
    comments: [],
    _id: 5c8ad0e8ef45f6e51a323a17,
    body: 'adf',
    created: 2019-03-14T22:08:40.336Z,
    __v: 0 } ]

什么? 我的電話是Object['newKey'] = await fsPromise.readFile(yadayada)其中await fsPromise.readFile(yadayada)顯示在 console.log 中工作。 為什么我不能這樣做,這沒有意義。

我剛剛做了一個小測試,如果在您的情況下您嘗試獲取要打印的“數據”屬性,您似乎應該能夠看到輸出:

console.log('after assignment and value of tempPost in asyncForEach: ',tempPosts[tempPosts.indexOf(tempPost)]['data'])

但是,除非您在 TempPost 的 mongoose Schema 中定義了該鍵,否則嘗試使用console.log(tempPost)不會顯示data

如果您想將 tempPost 作為普通的 javascript 對象操作,您需要通過調用toObject將 tempPost 模型文檔轉換為普通的 JavaScript 對象,例如。 tempPost = tempPost.toObject(); 在此之后,您的console.log('after assignment and value of tempPosts in asyncForEach: ', tempPosts)將給出預期的結果。

所以這與 async/await 和賦值沒有任何關系,imo

暫無
暫無

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

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