2016-11-08 208 views
2

我對Promises(第一次使用它們)不太滿意。有了其他一些幫助,我修改了一些代碼,但是我仍然在readFileThenAttach()方法的putAttachment()方法中發生了409個衝突錯誤。所以,我需要另一雙眼睛來看這個。我一直得到這個錯誤,然後我今天上午再次嘗試。它工作了兩次(我沒有得到錯誤)。我停止了程序,然後當我再次運行它時,我再次遇到了錯誤。我不確定有什麼問題。修訂版似乎沒問題,所以我不確定它是否有一些時間問題,或者我的代碼中的Promise如何。該代碼的執行路徑從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參數reverse ... filename應該先是rev。改變這個更正了這個問題。

+0

哇,這是很難找到!這仍然沒有在@typings lib中修復。 –