我对Promises不太好(第一次与他们合作)。在其他一些帮助下,我修改了一些代码,但我仍然在readFileThenAttach()方法的putAttachment()方法上遇到409冲突错误。所以,我需要另一组眼睛来为我看这个。我一直都是这个错误,然后我今天早上来了,再试一次。它工作了两次(我没有得到错误)。我停止了程序然后当我再次运行它时,我又得到了错误。我不确定是什么错。修改似乎没问题,所以我不确定它的时间问题是什么,或者Promise在我的代码中是怎么回事。此代码的执行路径从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')))
}
答案 0 :(得分:1)
对此的解决方案最终变得非常简单和愚蠢。项目中的PouchDB类型错误...它有putAttachment()的rev和filename参数反转...文件名应该是第一个然后转。改变这一点纠正了这个问题。