Export translation promise, what am I doing wrong

51 views
Skip to first unread message

Emiliano Heyns

unread,
May 27, 2021, 5:32:49 AM5/27/21
to zotero-dev

I'm trying to promisify export, using

  private rdf(): Promise<string> {
    return new Promise((resolve, reject) => {
      const items = Zotero.getActiveZoteroPane().getSelectedItems()
      if (items.length === 0) return resolve('')

      const translation = new Zotero.Translate.Export()
      translation.setItems(items)
      translation.setTranslator('5e3ad958-ac79-463d-812b-a86a9235c28f') // rdf

      translation.setHandler('done', (obj, success) => {
        if (success) {
          resolve(obj ? obj.string : undefined)
        }
        else {
          reject('translation failed')
        }
      })

      translation.translate()
    })
  }

but that gets me

(4)(+0000011): Translate: Binding sandbox to http://www.example.com/

(3)(+0000008): Getting contents of /Users/emile/.BBTZ5TEST/zotero/translators/RDF.js

(4)(+0000003): Translate: Parsing code for RDF (5e3ad958-ac79-463d-812b-a86a9235c28f, 2020-06-26 20:49:31)

(3)(+0000004): Translate: Initializing RDF data store

(3)(+0000001): Translate: Beginning translation with RDF

(3)(+0000001): serialize.js Find bnodes with only one incoming arc

(3)(+0000000): serialize.js Dummy serialize to check for missing nodes

(1)(+0000001): TypeError: Function.prototype.apply called on incompatible undefined rest@chrome://zotero/content/xpcom/translation/translate.js:1356:28 Zotero.Translate.Base.prototype._translateTranslatorLoaded</<@chrome://zotero/content/xpcom/translation/translate.js:1346:37

(2)(+0000001): Translate: Translation using RDF failed: TypeError: Function.prototype.apply called on incompatible undefined rest@chrome://zotero/content/xpcom/translation/translate.js:1356:28 Zotero.Translate.Base.prototype._translateTranslatorLoaded</<@chrome://zotero/content/xpcom/translation/translate.js:1346:37 tryCatcher@resource://zotero/loader.jsm -> resource://zotero/bluebird/util.js:16:16 module.exports/Promise.prototype._settlePromiseFromHandler@resource://zotero/loader.jsm -> resource://zotero/bluebird/promise.js:547:13 module.exports/Promise.prototype._settlePromise@resource://zotero/loader.jsm -> resource://zotero/bluebird/promise.js:604:13 module.exports/Promise.prototype._settlePromise0@resource://zotero/loader.jsm -> resource://zotero/bluebird/promise.js:649:5 module.exports/Promise.prototype._settlePromises@resource://zotero/loader.jsm -> resource://zotero/bluebird/promise.js:729:13 _drainQueueStep@resource://zotero/loader.jsm -> resource://zotero/bluebird/async.js:93:9 _drainQueue@resource://zotero/loader.jsm -> resource://zotero/bluebird/async.js:86:9 Async.prototype._drainQueues@resource://zotero/loader.jsm -> resource://zotero/bluebird/async.js:102:5 Async/this.drainQueues@resource://zotero/loader.jsm -> resource://zotero/bluebird/async.js:15:9 downloadAssociatedFiles => true automaticSnapshots => true

(5)(+0000000): Translate: Running handler 0 for error

(5)(+0000001): Translate: Running handler 0 for done

Dan Stillman

unread,
May 27, 2021, 5:43:15 AM5/27/21
to zoter...@googlegroups.com
On 5/27/21 5:32 AM, Emiliano Heyns wrote:
>
> I'm trying to promisify export, using
>
> […]
> translation.setTranslator('5e3ad958-ac79-463d-812b-a86a9235c28f') // rdf
> […]
>
> (3)(+0000008): Getting contents of
> /Users/emile/.BBTZ5TEST/zotero/translators/RDF.js
>
> […]
>
> (1)(+0000001): TypeError: Function.prototype.apply called on
> incompatible undefined
> rest@chrome://zotero/content/xpcom/translation/translate.js:1356:28
> Zotero.Translate.Base.prototype._translateTranslatorLoaded</<@chrome://zotero/content/xpcom/translation/translate.js:1346:37
>

You're trying to use RDF.js, which doesn't have a doExport() function,
instead of Zotero RDF (14763d24-8ba0-45df-8f52-b8d1108e7ac9), which does.

(But also, translate() is already async. You don't have to promisify it.)

Emiliano Heyns

unread,
May 27, 2021, 5:44:44 AM5/27/21
to zotero-dev
Good news on both of those then! Thank you!

Emiliano Heyns

unread,
May 27, 2021, 5:45:52 AM5/27/21
to zotero-dev
translation.translate returns the generated output then?

Dan Stillman

unread,
May 27, 2021, 5:55:07 AM5/27/21
to zoter...@googlegroups.com
On 5/27/21 5:45 AM, Emiliano Heyns wrote:
> translation.translate returns the generated output then?

No, it's not set up to do that. It'll just resolve when the translation
process is done.

An export can include attachments, so resolving to the string doesn't
totally make sense. But I suppose we could consider doing that when
setLocation() isn't used to set an output path.

Emiliano Heyns

unread,
May 27, 2021, 5:57:42 AM5/27/21
to zotero-dev
Understood. But for my purposes then, where I only want the text, the earlier promisification does the job.
Reply all
Reply to author
Forward
0 new messages