Search code examples
node.jsjsonexpressknex.js

How to Insert JSON data into 3 tables and respond 2 table's data through JSON using knex and express.js


Inserting JSON data into 3 tables and respond 2 table's data through JSON using knex and express.js

We want to Insert the data to login, user and profile table and then Respond the user and profile table data through json.

db.transaction(trx => {
      trx.insert({
          password: password,
          email: user.email,
          username: user.username
        })
        .into('login')
        .returning('username')
        .then(loginusername => {
             return trx('users')
                .returning('*')
                 .insert({
                  email: user.email,
                  username: loginusername,
                  name: user.name,
                  joined: new Date()
                  })
            .returning(user[0])
            .then(user => {
             return trx('profile')
                .returning('*')
                 .insert({
                  name: name,
                  image: image,
                  username: user.username
                  })
            .then(user => {
                 res.json(user[0], profile[0]);
                  })

          })

Solution

  • You were on the right track, just a bit of syntax issues that I've corrected. I also moved the table record structures out of the insert processing just for clarity.

    It is worth noting that .returning() does not work with SQLite nor MySQL. (See documentation here). If you are using either of those, then save and return the data from the inputs instead.

    // separated record data for clearer code below
    let loginData = { password: password,
                    email: user.email,
                    username: user.username };
    let userData = { email: user.email,
                    username: user.username,
                    name: user.name,
                    joined: new Date() };
    let profileData = { name: user.name,
                    image: image,
                    username: user.username };
    
    let retUserName = null; // need to track the .returning() data after the .then is complete
    let retUserData = null;
    let retProfileData = null;
    
    db.transaction(trx => {
        return trx.insert(loginData).into('login').returning('username')
            .then(retData => {
                retUserName = retData[0];  // save the data outside of .then
                return trx.insert(userData).into('users').returning('*')
            })
            .then(retData => {
                retUserData = retData[0];          // save the data outside of .then
                return trx.insert(profileData).into('profile').returning('*')
            })
            .then(retData => {
                retProfileData = retData[0];
                console.log({ 'userData': retUserData, 'profileData': retProfileData });
                // console.log to be replaced by your response:
                // res.json({ 'userData': retUserData, 'profileData': retProfileData });
            })
    });