5

I've been learning to use Angular 6 and NodeJS (with ExpressJS) these past few days. To do this, I decided to create a front-end with Angular 6 (o which theres a form) with the intention of passing information back and forth to my PHPMyAdmin database via the use of an API I created with NodeJS. As you may have seen from the title, I get the following error when I try to submit my forms information:

"Unhandled rejection TypeError: callback is not a function"

The code I used for the post action was taken from another functioning and similar API, the database receives the question_id and user_id but will not register the questions answers (which is the end goal). Without furthur ado, here is the code to get and post:

const config = require('../config/config');
const knex      = config.db.knex;
const async     = require('async');


class TestModel {        
  getQuestions(callback) {
        knex('question')
        .select('id', 'libelle')
    .orderBy('id', 'asc')
        .then((data) => {
            callback(data);
        });
    }


  addResponse(reponse, callback) {
      knex('reponse')
      .insert({
          id_user : 1,
          id_question : reponse.id,
          libelle: reponse.answer,
      }).then((data) => {
          callback(data);
        });
  }        
}


module.exports = new TestModel();

Here is the rest:

app.post('/quiz', function(req, res, next){
    var answers = req.body;

    console.log(answers)

    for(var i = 0; i<answers.length; i++)
        {
            var obj = answers[i];
            Test.addResponse((obj,result) => {

            });                
        }        
    res.json({reponseserveur:'True'});    
});

Just for reference, "reponse" means response and "libelle" means label. Thanks in advance for your help!

2 Answers 2

2

The correct Syntax is

        Test.addResponse(obj, (result) => {

        }); 

it's working:

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thanks!
1
Test.addResponse((obj,result) => {})

Looks like you're giving your callback as the first parameter (instead of response). It should be 2nd.

You probably wanted to do:

Test.addResponse(res, (obj,result) => {})

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.