作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我不太擅长 Promises(第一次使用它们)。在其他一些帮助下,我修改了一些代码,但我仍然在 readFileThenAttach() 方法中的 putAttachment() 方法上收到 409 冲突错误。所以,我需要另一双眼睛来帮我看看这个。我一直收到这个错误,然后我今天早上回来再试一次。它工作了两次(我没有收到错误)。我停止了程序,然后当我再次运行它时,我又遇到了错误。我不确定出了什么问题。修订似乎没问题,所以我不确定它是否存在时间问题或者我的代码中的 Promises 是怎样的。此代码的执行路径从 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')))
}
最佳答案
这个问题的解决方案最终变得非常简单和愚蠢。项目中的 PouchDB 类型是错误的……它有 putAttachment() 的 rev 和文件名参数颠倒了……文件名应该先是然后是 rev。更改此项可以解决问题。
关于node.js - CustomPouchError 409 Conflict 文档更新冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40493550/
我不太擅长 Promises(第一次使用它们)。在其他一些帮助下,我修改了一些代码,但我仍然在 readFileThenAttach() 方法中的 putAttachment() 方法上收到 409
我是一名优秀的程序员,十分优秀!