alfresco-js-api icon indicating copy to clipboard operation
alfresco-js-api copied to clipboard

getNodeContent response returns empty object on Node.js 11

Open sanzoghenzo opened this issue 4 years ago • 2 comments

Type of issue: (check with "[x]")

- [ ] New feature request
- [x] Bug  
- [ ] Support request

Current behavior:

I get an empty object if I try to use nodesApi.getNodeContent(nodeid) on a node.js script with a DXF file (well, the only file I have on this alfresco test environment).

Using Postman with to call /nodes/{nodeid}/content works as expected.

I'm aware of #271 and that #278 corrects this for the legacy api, but getNodeContent doesn't pass returnType = 'Binary' to callHostApi (if it's still the case).

Expected behavior:

I should get the stream of the file.

Steps to reproduce the issue:

  • npm install @alfresco/js-api dotenv
  • create a .env file with:
ALFRESCO_ECM_URI={{your ecm uri}}
ALFRESCO_USER={{your username}}
ALFRESCO_PASSWORD={{your password}}

Run this code

const {
  AlfrescoApi,
  SearchApi,
  NodesApi,
  ContentApi
} = require('@alfresco/js-api')

const Dotenv = require('dotenv')

Dotenv.config({ silent: true })

const { ALFRESCO_ECM_URI, ALFRESCO_USER, ALFRESCO_PASSWORD } = process.env
const alfresco = new AlfrescoApi({ hostEcm: ALFRESCO_ECM_URI })
const searchApi = new SearchApi(alfresco)
const nodesApi = new NodesApi(alfresco)
const contentApi = new ContentApi(alfresco)

async function login() {
  if (!alfresco.isLoggedIn()) {
    try {
      await alfresco.login(ALFRESCO_USER, ALFRESCO_PASSWORD)
    } catch (err) {
      console.error('Alfresco login error.')
      return false
    }
  }
  return true
}

async function searchFiles(query, maxItems = 100, skipCount = 0) {
  const queryBody = {
    query: { query },
    paging: {
      maxItems,
      skipCount
    },
    fields: ['id', 'name']
  }
  const { list } = await searchApi.search(queryBody)
  const { entries } = list
  if (entries.length > 0) {
    return entries.map(x => x.entry)
  } else {
    console.warn('No results found.')
  }
}

async function getFile(nodeId) {
  const opts = { attachment: true }
  const data = await nodesApi.getNodeContent(nodeId, opts)
  return data
}

async function main() {
  const projectSite = 'big-project' // change it to an existing site name
  const code = 'technicalDrawing001'  // change it to some existing file name or content
  const query = `+SITE:'${projectSite}' AND EXACTTYPE:'cm:content' AND (cm:name:${code} cm:content:${code})`
  const loggedIn = await login()
  if (loggedIn) {
    const results = await searchFiles(query)
    if (Array.isArray(results) && results.length > 0) {
      for (let entry of results) {
        const { id, name } = entry
        const url = await contentApi.getContentUrl(id)
        console.log(`id: ${id}`)
        console.log(`name: ${name}`)
        console.log(`url: ${url}`)
        const file = await getFile(id)
        console.log(file)
      }
    }
  }
}

main()
  .then(console.log('Done.'))
  .catch(err => console.error(err))

Node version (for build issues):

v11.9.0

sanzoghenzo avatar Aug 02 '19 16:08 sanzoghenzo

Can you retry with last version?

eromano avatar Dec 27 '19 19:12 eromano

Hi, sorry for the late reply. we don't use this feature ATM, but I just retried with a fresh installation of node LTS 12.16.1 and js api, but nothing changed.

sanzoghenzo avatar Mar 26 '20 14:03 sanzoghenzo

closing as Node 18 is used. please reopen with more details if this is still an issue

DenysVuika avatar Jun 16 '23 09:06 DenysVuika