Start a new topic

response.complete multiple times error

I have a custom endpoint that runs the following code:



users.find( {

"$or": [

{ "name": q },

{ "username": q }

]

}, function( error, docs ){

if( !error )

{

var result = [];

docs.forEach( function( user ){

result.push( {

_id: user._id,

name: user.name || user.username,

profilePicture: user.profilePicture,

username: user.username

} );

});

response.body = { "users": result };

response.complete( 200 );

}

else

{

response.body.reason = "Error finding users";

response.error( response.body.reason );

}

});



It was working fine until feel minutes ago. Suddenly, it started to return an error saying: reason: response.complete() or response.continue() was called multiple times, remaining time: 2701



It's a simple process and I can't understand why I'm getting this error.

Any ideas?

thanks!

Neto,



I don't have a definitive answer. Just one suggestion. On your response.complete or response.error use a return as well. Doing a return should definitely tell Express not to do any continue process.



FYI: If you indent your code with 4 spaces, it will be formatted for easy viewing (Markdown).



users.find( {

"$or": [

{ "name": q },

{ "username": q }

]

}, function( error, docs ){

if( !error )

{

var result = [];

docs.forEach( function( user ){

result.push( {

id: user.id,

name: user.name || user.username,

profilePicture: user.profilePicture,

username: user.username

} );

});

response.body = { "users": result };

return response.complete( 200 );

}

else

{

response.body.reason = "Error finding users";

return response.error( response.body.reason );

}

});
Yea, I agree with Justin's answer.

I ended up in the same problem a few weeks back... and I couldn't find the reason either.



So, the first solution I adopted was adding a "return", as Justin suggested too. And that works perfectly if everything is inside of the onRequest() function.



Then I realized that my business code was quite complex, having multiple functions called by a main one... (and most of them asynchronous) return wasn't enough safe.

So I decided to call the "complete" only once in my whole code, from inside of another function, and use a flag to make sure that'll be called only once.



var stop = false;

function complete(body, code) {

if(!stop) {

stop = true;

if(!code) code = 200;

_response.body = body;

_response.complete(code);

}

}



this way in my code I only call " complete (results) " and I know it'll be safe now ;)
Thank you guys! That really helped me!
Guys, I adopted Davide suggestion but my app is still getting blacklisted =/

The remaing time of the block ended and I started to testing. But it worked for 2 minutes and got blocked again.



Do you think that the problem is related with my current billing plan? Actually I'm using the free version yet. I just realized that it allows only 5 business logic scripts. Would it be the problem?



Thanks again!
only 5 BL scripts? o.O wow......

I don't know. This is new things on Kinvey and I'm not sure how they're setting them up.

Tho, I guess that if it's blacklisting because of multiple callings to complete(), then it must be that there's still multiple ones... hmm... are you still calling response.error? those should be called with the function too... I do something like: "complete(err, 400)" for example.

And, was the one you posted earlier on the full code or is there something more?



sorry that you get blacklisted, I know how annoying it is :D
Neto - you should check your other BL functions to see if response.complete is being called multiple times. It may be another endpoint or hook that is doing it.



From the looks of this code, it doesn't seem like it would cause the violation.



Also - just a best practice note: instead of using the javascript forEach, you should utilize the async module's each method, which can then call a callback upon completion of the iteration.
Login or Signup to post a comment