Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promises don't seem to work #32

Open
konsumer opened this issue Jan 14, 2017 · 5 comments
Open

Promises don't seem to work #32

konsumer opened this issue Jan 14, 2017 · 5 comments

Comments

@konsumer
Copy link

konsumer commented Jan 14, 2017

In node v7.2.0, code like this works:

client.projects.list({}, (err, projects) => {
  if (err) throw err
  console.log(projects)
})

But with this:

client.projects.list({})
  .then(projects => console.log(projects))
  .catch(err => { throw err })

I get this error:

/Users/konsumer/Desktop/migrate-git/migrate.js:45
  .then(milestones => console.log(milestones))
  ^

TypeError: Cannot read property 'then' of undefined
    at Object.<anonymous> (/Users/konsumer/Desktop/migrate-git/migrate.js:45:3)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:535:3

It seems like promises don't work with several others, as well.

@zivni
Copy link

zivni commented Jan 18, 2017

Tested the following in node v7.4.0 and it works.

did you use gitlab.createPromise?

var gitlab = require('node-gitlab');

var client = gitlab.createPromise({
    api: 'http://gitlab..../api/v3',
    privateToken: 'z....m'
});

client.projects.list({})
    .then(p => console.log(JSON.stringify(p)))
    .catch(err => console.error(err))

@konsumer
Copy link
Author

ah, nope. thanks!

@kristianmandrup
Copy link

kristianmandrup commented May 14, 2017

Sweet :)

@kristianmandrup
Copy link

kristianmandrup commented May 19, 2017

I've been having problems with Promises as well...
Added the commitActions method to repository. Please let's get this method added! Help me out :) This is a nightmare/jungle. Spent all day...

my fork

gitlabl: create a commit with multiple files and actions

Finally got it working, but only in callback mode. I need promises to work with the rest of my code!

  it('should commit a list of actions', function (done) {
    console.log(`repository.commitActions`, client.id)
    let random = Math.floor((Math.random() * 1000) + 1)
    client.repository.commitActions({
      id: client.id,
      // branch_name: 'develop', for v3 API
      branch: 'develop',
      actions: [{
        action: 'create',
        file_path: `foolish-${random}`,
        content: 'some content'
        // encoding: 'text'
      }],
      // author_email: 'test@gmail.com',
      // author_name: 'tester',
      commit_message: 'goodies'
    }, function (err, res) {
      console.log('RETURNED', {
        err,
        res
      })
      should.not.exists(err);
      should.exists(res);
      done();
    })
  })

However if I wrap it in a Promise, I always get this error:

{ Gitlab400Error: You can only create or edit files when you are on a branch
    at Gitlab.RESTFulClient.handleResult (/Users/kristianmandrup/repos/tecla5/gitlab/node_modules/restful-client/lib/client.js:86:11)
    at /Users/kristianmandrup/repos/tecla5/gitlab/node_modules/restful-client/lib/client.js:166:10
    at done (/Users/kristianmandrup/repos/tecla5/gitlab/node_modules/urllib/lib/urllib.js:266:5)
    at /Users/kristianmandrup/repos/tecla5/gitlab/node_modules/urllib/lib/urllib.js:440:9
    at IncomingMessage.<anonymous> (/Users/kristianmandrup/repos/tecla5/gitlab/node_modules/urllib/lib/urllib.js:416:7)

Even when wrapping manually:

    function errorAndReject(err, reject) {
      console.error(err)
      reject(err)
    }

    function commitActionsPromised(data) {
      return new Promise((resolve, reject) => {
        client.repository.commitActions(data, (err, result) => {
          err ? reject(err, reject) : resolve(result)
        })
      })
    }

    // essentially should be same as:
    // client.promise.repository.commitActions
    commitActionsPromised({
        id: client.id,

Any idea? I also tried using client.promise.repository.commitActions ensuring the commitActions was added to repositories object of properties.js to be picked up by promisifyAll

My before and after to create and cleanup test project (i.e client.id)

const cleanupTimeout = 1000

  before(function (done) {
    // use API v4 ??
    // See: https://docs.gitlab.com/ce/api/v3_to_v4.html
    // process.env.NODE_GITLAB_API = 'https://gitlab.com/api/v4'

    console.log('before suite, create fresh test project to be used :)')
    client.createProject(function (err) {
      console.log(`test project created: ${client.id}`)
      console.log(`Ready for action!`)
      done();
    });
  });

  // Seems to work only if I don't clean up (remove) project after.
  // Looks like a race condition!?
  after((done) => {
    // Try cleanup 5secs after tests done
    setTimeout(() => {
      console.log(`Cleaning up!!!`)
      client.removeProject()
      console.log(`DONE Clean up!!!`)
      done()
    }, cleanupTimeout)
  });

I also experienced racing condition on after cleanup, so I set a 1 sec wait.

@kristianmandrup
Copy link

Turns out the weird error was most likely due to slow/bad wifi connection. When I moved my laptop closer to the router this morning, it all worked as expected :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants