isanma
isanma

Reputation: 175

Cannot read property then of undefined when accessing own api

I am trying to use my own express.js api with nodejs. The problem is that it works but it is giving an error and I cannot access the result of the petition. This is my code:

routes.js:

app.post('/petition/:id', function(req, res) {
    console.log("ID: ", req.params.id);
    if (!req.params.id) {
        return res.send({"status": "error", "message": "Chooser id needed"});
    }
    else {
        indicoUtils.indicoPositivosNegativos(req.params.id).then(function(result) {
            return res.send({"result": result});
        })
    }
})

calculator.js:

var indicoPositivosNegativos = function (chooserId) {
    var TweetModel = mongoose.model('Tweet'.concat(chooserId), Tweet.tweetSchema);
    TweetModel.find({},{ _id: 1, tweet: 1}).then(tweets =>
        Promise.all(
            tweets.map(({ _id, tweet }) =>
                indico.sentiment(tweet).then(result =>
                    TweetModel.findOneAndUpdate({ _id }, { indicoPositivoNegativo: result }, { new: true })
                        .then( updated => { console.log(updated); return updated })
                )
            )
        )
    )
};

I am testing this with Postman and it shows the error:

TypeError: Cannot read property .then of undefined

Upvotes: 0

Views: 423

Answers (2)

Manos Agelidis
Manos Agelidis

Reputation: 91

This basically means that one of the objects that you are trying to call the .then function is undefined.

Specifically the object indicoUtils.indicoPositivosNegativos(req.params.id) should be a promise, but your function indicoPositivosNegativos does not return a promise. In fact your function does not return anything, hence the .then is called on an undefined value.

The solution is simple, you have to add a return statement on the calculator.js in order to return a promise like this:

var indicoPositivosNegativos = function (chooserId) {
    var TweetModel = mongoose.model('Tweet'.concat(chooserId), Tweet.tweetSchema);
    return TweetModel.find({},{ _id: 1, tweet: 1}).then(tweets =>
        Promise.all(
            tweets.map(({ _id, tweet }) =>
                indico.sentiment(tweet).then(result =>
                    TweetModel.findOneAndUpdate({ _id }, { indicoPositivoNegativo: result }, { new: true })
                        .then( updated => { console.log(updated); return updated })
                )
            )
        )
    )
};

Upvotes: 1

Jason Cust
Jason Cust

Reputation: 10899

The promise created by TweetModel.find is not returned to the calling function from the route handler.

var indicoPositivosNegativos = function(chooserId) {
  var TweetModel = mongoose.model('Tweet'.concat(chooserId), Tweet.tweetSchema);
  // THE PROMISE NEEDS TO BE RETURNED FOR CALLING FUNCTIONS
  // TO HAVE ACCESS TO IT.
  return TweetModel.find({}, { 
    _id: 1,
    tweet: 1
  }).then(tweets =>
    Promise.all(
      tweets.map(({
          _id,
          tweet
        }) =>
        indico.sentiment(tweet).then(result =>
          TweetModel.findOneAndUpdate({
            _id
          }, {
            indicoPositivoNegativo: result
          }, {
            new: true
          })
          .then(updated => {
            console.log(updated);
            return updated
          })
        )
      )
    )
  )
};

Upvotes: 0

Related Questions