2016-11-08 15 views
2

私はPromises(最初に作業しています)とあまりよくはありません。他の助けを借りて、コードを修正しましたが、readFileThenAttach()メソッドのputAttachment()メソッドで409の競合エラーが発生しています。だから、私のためにこれを見るためにもう一組の目が必要です。私は一貫してこのエラーを受けていました、そして、私は今朝来て、もう一度試しました。それは2回働いた(私はエラーを得なかった)。私はプログラムを停止し、再度実行したときにエラーが再び発生しました。私は何が間違っているのか分かりません。リビジョンは大丈夫だと思われるので、タイミングの問題があるのか​​、約束がどのように私のコードに含まれているのかは分かりません。このコードの実行パスはimportProject()メソッドから始まり、そこからimportInspectionPhotos()が呼び出されます。誰かが目立つものが見えるかどうか見てみることができますか?ありがとう。CustomPouchError 409競合しているドキュメントの更新の競合

function buildReinspectionLinks(db: InspectionDb) { 
    return db.allObservations() 
     .then(([points, lines]) => { 
      let observations = new Map([...points, ...lines] 
       .filter(obs => (<any>obs).access_original) 
       .map<[string, Observation]>(obs => [(<any>obs).access_id, obs])) 
      let changed = new Set() 
      for (let obs of observations.values()) { 
       let doc = (<any>obs).access_original 
       if (doc.Inspect_ID != doc.Original_ID) { 
        let reinspected = observations.get(doc.Original_ID) 
        doc.reinspected_id = reinspected._id 
        reinspected.reinspected = true 
        if (!reinspected.reinspection_ids) { 
         reinspected.reinspection_ids = [] 
        } 
        reinspected.reinspection_ids.push(obs._id) 
        changed.add(obs) 
        changed.add(reinspected) 
       } 
      } 
      // TODO: Recurse the relationships? 
      return Promise.all([...changed].map(obs => db.post(obs))) 
     }) 
} 

function importInspectionPhotos(db: InspectionDb, directoryBase: string) { 

const observations = db.allObservations().then(([points, lines]) => new Map([...points, ...lines].filter(obs => (<any>obs).access_original).map<[string, Observation]>(obs => [(<any>obs).access_id, obs]))) 
const filenames = globP("**/*.{jpg, jpeg, gif, png}", { cwd: directoryBase }) 

return Promise.all([observations, filenames]).then(([obs, names]: [Map<string, Observation>, string[]]) => { 

    const fileObservations: FileObservation[] = names.map(file => { 
     const filename = basename(file) 
     const accessID = getAccessObservationId(filename) 
     return { 
      file, 
      path: `${directoryBase}/${file}`, 
      observation: obs.get(accessID) 
     } as FileObservation 
    }).filter((fileOb: FileObservation) => !!fileOb.observation) 

    return fileObservations.reduce((lastPromise, fileOb) => lastPromise.then(() => readFileThenAttach(db, fileOb)), Promise.resolve()) 
    }) 
} 

function getAccessObservationId(filename: string): string { 

return filename.substr(0, filename.lastIndexOf('_')) 
} 

function readFileThenAttach(db: InspectionDb, fileOb: FileObservation): Promise<any> { 

return readFileP(fileOb.path) 
    .then((data: Buffer) => blobUtil.arrayBufferToBlob(data.buffer, contentType(extname(fileOb.path)))) 
    .then(blob => ({ content_type: blob.type, data: blob }) as PouchAttachment) 
    .then(pa => db.putAttachment(fileOb.observation._id, (fileOb.observation as any)._rev, fileOb.filename, pa.data, pa.content_type)) 
    .then(update => ((fileOb.observation as any)._rev = update.rev)) 
} 

function importData(filename: string, db: InspectionDb, table: string, importer: (db: InspectionDb, doc: any) => Promise<any>) { 
return new Promise((resolve, reject) => { 
    let trader = spawn(TraderPath, ['select', `-f="${filename}"`, `-t=${table}`]) 

    trader.stderr.on('data', reject) 
    trader.on('error', reject) 

    let outstream = [] 
    trader.stdout.on('data', data => outstream.push(data)) 

    var imports = [] 
    trader.on('close', code => { 
     if (code > 0) reject('Trader returned non-zero exit code') 
     safeLoadAll(''.concat(...outstream), (doc: any) => imports.push(importer(db, doc))) // safeLoadAll is synchronous 
     Promise.all(imports).then(resolve) 
    }) 
}) 
} 

export function importProject(filename: string, project_id: string) { 
let db: InspectionDb 

return Promise.resolve() 
    .then(() => {   
     db = new InspectionDb(project_id) 

     return Promise.all([ 
      importData(filename, db, 'Project_Inspections', importInspection), 
      importData(filename, db, 'Inspection', importObservation), 
     ]) 
    }) 
    .then(() => buildReinspectionLinks(db)) 
    .then(() => importInspectionPhotos(db, join(dirname(filename), '../Inspection_Projects'))) 
} 

答えて

1

これに対する解決策は非常に単純で愚かであった。プロジェクトのPouchDBタイピングは間違っていました... putAttachment()のrevとfilenameパラメータを逆にしました... filenameは最初にrevだったはずです。これを変更すると問題が解決しました。

+0

わかりにくい!これはまだ@typings libで修正されていません。 –