【问题标题】:Async/Await to an Object Not Assigning异步/等待未分配的对象
【发布时间】:2023-03-04 20:01:01
【问题描述】:

我有一个未分配给对象的 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 中工作。为什么我不能这样做,这没有意义。

【问题讨论】:

  • after assignment and value of tempPosts in asyncForEach: 输出到底有什么问题?我无法发现问题。
  • 它应该显示一个字段,其中包含一个名为 data 的键,其中包含数据。与 console.log 中显示的数据缓冲区一样。
  • FWIW, tempPost.data = await ... 会更容易。如果您使用console.log(tempPost.data) 而不是console.log(tempPosts) 会怎样?还有这些对象是什么?他们是否可能已经有一个无法覆盖的特殊 data 属性?
  • 是的,我尝试使用tempPost.data 来代替,结果相似。这些对象是猫鼬模型,特别是没有数据条目。我想我的下一步将是用空数据条目实例化 mongoose 模型,并查看初始化时缺少条目是否会阻止分配。

标签: javascript node.js mongoose properties


【解决方案1】:

我刚刚做了一个小测试,如果在您的情况下您尝试获取“数据”属性进行打印,您应该能够看到输出:

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

但是尝试 console.log(tempPost) 将不会显示 data 除非您在 TempPost 的 mongoose Schema 中定义了该键

如果您想将 tempPost 操作为纯 JavaScript 对象,您需要通过调用 toObject 将 tempPost 模型文档转换为纯 JavaScript 对象,例如。 tempPost = tempPost.toObject(); 在此之后您的 console.log('after assignment and value of tempPosts in asyncForEach: ', tempPosts) 将给出预期的结果。

所以这与异步/等待和赋值无关,imo

【讨论】:

  • 答案显然是,您不能向对象添加值即使它是从猫鼬模式中提取的。只要您阅读该值,我就认为这是可能的,但显然情况并非如此。
猜你喜欢
  • 2017-10-17
  • 1970-01-01
  • 1970-01-01
  • 2020-12-17
  • 1970-01-01
  • 2017-12-02
  • 2019-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多