-1

i am trying to insert data from js object in a database using request-promise and update the object with the response of the POST but i don't know how to get my updated object in the last .then()

I don't understand very good promises and have no idea how to manage the if ... else

var rp = require('request-promise')

var test_films = require("./films.json")

function searchPerson(persons, firstname, lastname) {
  return persons.filter(function (p) {
    return p.firstname == firstname && p.lastname == lastname
  })
}

var options = {
  uri: 'http://my-api',
  json: true
}

rp(options)
.then(function (persons) {
  test_films.forEach(function(comedien) {

    var found = searchPerson(persons, comedien.firstname, comedien.lastname)

    if (found.length === 0) {
      console.log('user: ' + comedien.firstname + ' ' + comedien.lastname + ' not found, insert it!')

      options = {
        method: 'POST',
        uri: 'http://my-api',
        formData: {
          firstname: comedien.firstname,
          lastname: comedien.lastname
        },
        json: true
      }
      // POST actor and insert id created in array
      rp(options)
      .then(function (body) {
        comedien.person_id = body.id
      })
      .catch(function (err) {
        console.log(err)
      })
    }
    else {
      console.log('User: ' + comedien.firstname + ' ' + comedien.lastname + ' already exists!')
      comedien.person_id = found[0].id
    }
  })
  return test_films
})
.then(function (result) {
  // The object is not updated after i insert it in database
  console.log(result)
})
.catch(function (err) {
  console.log(err)
})

Thanks for your help !

PS: Tell me why you downvote i an try to improve my question !

amerej
  • 139
  • 1
  • 11
  • Partial duplicate of this: https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call/14220323#14220323, but also you need to `return` nested promises from within the `.then()` handlers in order to chain them so the higher level `.then()` handlers will wait for them and include their result. – jfriend00 Nov 03 '17 at 18:57

1 Answers1

-1

*Warning: I am a relative JS newbie myself, but since no one else has answered yet, I think I can offer some help in the meantime - not tons, I'm not an expert, but maybe enough to point you in the right direction.

The issue is how you are trying to chain .then() together, one right after another. In order to chain another .then() at the end, the first .then() has to return a function (and that function can return your test_films). As it stands, your existing .then() just returns the variable test_films - it needs to return a whole function.

Take a look at the 2nd answer in this thread: How to chain .then functions and callback success function in Angular.js

The corrected code in that 2nd answer has multiple .then(), chained correctly with returns. Unfortunately, I'm not skilled enough to be able to walk you through line-by-line, you'll have to look up more examples of promises.

JeremyW
  • 5,157
  • 6
  • 29
  • 30