【问题标题】:CustomPouchError 409 Conflict Document Update ConflictCustomPouchError 409 冲突文档更新冲突
【发布时间】:2017-03-22 11:18:33
【问题描述】:

我对 Promises 不太擅长(第一次使用它们)。在其他帮助下,我对代码进行了一些修改,但在 readFileThenAttach() 方法中的 putAttachment() 方法上仍然出现 409 冲突错误。所以,我需要另一双眼睛来帮我看这个。我一直收到这个错误,然后我今天早上进来再试一次。它工作了两次(我没有得到错误)。我停止了程序,然后当我再次运行它时,我再次收到错误。我不确定出了什么问题。修订似乎没问题,所以我不确定它是否存在时间问题或者我的代码中的承诺如何。此代码的执行路径从 importProject() 方法开始,然后调用 importInspectionPhotos()。有人可以看看他们是否能看到任何突出的东西吗?谢谢。

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')))
}

【问题讨论】:

    标签: node.js typescript electron pouchdb


    【解决方案1】:

    这个问题的解决方案最终变得非常简单和愚蠢。项目中的 PouchDB 类型错误...它的 putAttachment() 的 rev 和文件名参数颠倒了...文件名应该是第一个然后是 rev。更改此设置可以解决问题。

    【讨论】:

    • 哇,好难找! @typings 库中仍未修复此问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多