Asynchronous problem async-awit

this problem is difficult to describe, but I"ll try to

clipboard.png

,
orm(typeOrm),mock
ProjectPanorama(ProjectPanorama)

3Panorama(mockData.defaultPanoramas)

  1. 3Panorama3ProjectPanorama

ProjectPanorama

projectId panoramaId
1 3
1 3
1 3

3panorama,

,let proj_pano,const
clipboard.png

the result becomes normal

< table > < thead > < tr > < th > projectId < / th > < th > panoramaId < / th > < / tr > < / thead > < tbody > < tr > < td > 1 < / td > < td > 1 < / td > < / tr > < tr > < td > 1 < / td > < td > 2 < / td > < / tr > < tr > < td > 1 < / td > < td > 3 < / td > < / tr > < / tbody > < / table >

it is because of this phenomenon that I suspect that the problem is async, not the orm library

< hr >

possible reasons

due to the first await in forEac

panoramas.forEach(async (p, j) => {
    proj_pano = new ProjectPanorama()
    proj_pano.project = project
    proj_pano.panorama = p
    
    // marker
    const markerToPP = []
    for (let k = 1; k <= 3; k += 1) {
      await marker.save()  // await
    }
    proj_pano.markers = markerToPP
    await proj_pano.save()
    // project.projectPanoramas.push(proj_pano)
})

causes the program to execute like this

proj_pano = new ProjectPanorama()
proj_pano.project = project
proj_pano.panorama = p
const markerToPP = []
// await,forEach
// .....
proj_pano = new ProjectPanorama() // proj_pano
proj_pano.project = project
proj_pano.panorama = p
const markerToPP = []
// ....
// await
// proj_pano,proj_pano
proj_pano.markers = markerToPP // markerToPP,
await proj_pano.save() //ormsave,savemarkers,
//proj_pano
proj_pano.markers = markerToPP // 
await proj_pano.save()
//proj_pano
proj_pano.markers = markerToPP // 
await proj_pano.save()

also do not use await
https://stackoverflow.com/que.

in forEach
Mar.10,2021

is not an asynchronous problem, nor is it solved by const , it's just that const can no longer be assigned. It's a scope problem (closure). Put several let in the loop for and foreach .

for(...){
    let xx //
}

I don't want to reply to

in the middle of the picture.
  • Why doesn't the async/await here work?

    writes a class that wants to read the data from a file and then store the data in the database. saver.connectMongo() .then(async () => { const data = await saver.getFileContent(filename); console.log(data); saver.saveCodeToMongo1(data...

    May.18,2022
Menu