0

I am creating an insert script that does some business logic.

Basically, I want to check to see if a value in the inserted item exists in a table. But, it seems like if I find a problem Request.Send() doesn't stop execution and get an error.

I think there is an async issue here. I'm not 100% sure how to solve.

Is there a way to stop execution of the script?

    if (item.memberType === 'Family' && item.primaryFamilyMember) {
    table
        .where({ 
            memberNumber: item.primaryFamilyMember, 
            memberType: 'Family', 
            primaryFamilyMember: null })
        .read({
            success: function(results) {                    
                if (results.length == 0) {
                    request.respond(statusCodes.BAD_REQUEST,
                        'Invalid Primary Family Member specified.');
                    console.error('Invalid Primary Family Member specified:' + item.primaryFamilyMember);
                    validInsert = false;
                } else {
                    item.memberType = results[0].memberType; 
                    item.memberLevel = results[0].memberLevel;
                    item.dateOfExpiry = results[0].dateOfExpiry;                        
                }
            }
        });
}

if (validInsert) {
    var today = new Date();
    var prefix = today.getFullYear().toString().substr(2,2) + ('0' + (today.getMonth() + 1)).slice(-2);

    table.includeTotalCount().where(function(prefix){
                return this.memberNumber.substring(0, 4) === prefix; 
            }, prefix)
            .take(0).read({
        success: function (results) {
            if (isNaN(results.totalCount)) {
                results.totalCount = 0;
            }            
            item.memberNumber = prefix + ('00' + (results.totalCount + 1)).slice(-3);
            request.execute();            
        }
    });
}

Yes, validInsert is declared at the top of the insert function.

I assume what's happening is the if(validInsert) runs before the read callback. But if so, i'm not sure why I'm getting "Error: Execute cannot be called after respond has been called." That implies the callback is running first.

Also, the record is being inserted when it shouldn't be even though the 400 error is sent back to the client.

This is an express app right? Should I just call response.end() after the error occurs?

1 Answer 1

1

Yes, there are definitely asyn issues in that code. To solve get rid of your validInsert flag and simply move the if (validInsert) section into the success callback (or make it a function called from the success callback). For example:

success: function(results) {
    if (results.length == 0) {
        request.respond(statusCodes.BAD_REQUEST,
            'Invalid Primary Family Member specified.');
        console.error('Invalid Primary Family Member specified:' + item.primaryFamilyMember);
    } else {
        item.memberType = results[0].memberType;
        item.memberLevel = results[0].memberLevel;
        item.dateOfExpiry = results[0].dateOfExpiry;

        var today = new Date();
        var prefix = today.getFullYear().toString().substr(2,2) + ('0' + (today.getMonth() + 1)).slice(-2);
        ...
        //respond successfully 

    }
}    
1
  • Yes thanks. I made it a function and called it from the call back or from the call back and from the else of the outter if and that solved the issue.
    – PilotBob
    May 29, 2015 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.