cowboys2288
cowboys2288

Reputation: 11

NodeJS Express REST API insert into MYSQL error

Hi I am trying to insert a record into a MySQL DB from a post (using postman to test) for a REST API. I keep receiving a server error. Here is my code (using async and await and expecting a promise in return). Here is the router:

 *  Creates a new user.
 */
router.post('/', async (req, res, next) => {
  const options = {
    body: req.body
  };

  try {
    const result = await user.createUser(options);
    res.status(result.status || 200).send(result.data);
  } catch (err) {
    next(err);
  }
});

And here is the Service (UPDATED - STILL ERROR):


   sql = "INSERT INTO users (AccountHolderUserID, isSubUser, parentUsersID, appBrand, accessLevel, accessToken, tempPassword, email, firstName, lastName) VALUES ?";

   values = [
     [options.body.AccountHolderUserID, options.body.isSubUser, options.body.parentUsersID, options.body.appBrand, options.body.accessLevel, options.body.accessToken, options.body.tempPassword, options.body.email, options.body.firstName, options.body.lastName] 
   ];

  console.log(values);

  pool.query(sql, [values], function (error, result, fields) {

  if (error) {
      reject(new ServerError({
        status: 500, // Or another error code.
        error: 'Server Error' // Or another error message.;
      }));
      return
    }
    resolve({
      status: 200,
      data: result
    });
  })
});
}

I added a console log right before the pool.query, and here is the response when I try to post. Data is making it from the form to the query, but getting an Cannot convert object to primitive value Error???

App listening on port 8082!
[
  [
    '1234, ',
    'true,',
    '1,',
    '1,',
    '1,',
    '1312,',
    '1234,',
    '[email protected],',
    'Nancy,',
    'Flannagan'
  ]
]
TypeError: Cannot convert object to primitive value

Please let me know if anyone knows what I am doing wrong. Thank you!

Upvotes: 1

Views: 470

Answers (2)

cowboys2288
cowboys2288

Reputation: 11

SOLVED AS FOllOWS:

/**
 * @param {Object} options
 * @param {Object} options.body Created user object
 * @throws {Error}
 * @return {Promise}
 */
module.exports.createUser = async (options) => {
return new Promise((resolve, reject) => {
const User = {
    "AccountHolderUserID": options.body.ctiAccountHolderUserID, 
    "isSubUser": options.body.isSubUser,
    "parentUsersID": options.body.parentUsersID,
    "appBrand": options.body.appBrand,
    "accessLevel": options.body.accessLevel,
    "accessToken": options.body.accessToken,
    "tempPassword": options.body.tempPassword,
    "email": options.body.email,
    "firstName": options.body.firstName,
    "lastName": options.body.lastName
  }
  console.log(User);
  pool.query('INSERT INTO users set ?', User,  function (error, result, fields) {
  if (error) {
      reject(new ServerError({
        status: 500, // Or another error code.
        error: 'Server Error' // Or another error message.;
      }));
      return
    }
    resolve({
      status: 200,
      data: result
    });
  })
});
}

Upvotes: 0

afenster
afenster

Reputation: 3608

I think MySQL expects each individual value to have its own question mark. You are passing an array of values expecting that it will substitute the whole VALUES clause, but AFAIK it has never worked this way. The error message says that it expects a primitive value and not an object (not an array, it probably wanted to say).

Try this:

sql = "INSERT INTO users " +
  "(AccountHolderUserID, isSubUser, parentUsersID, appBrand, " +
  " accessLevel, accessToken, tempPassword, email, " +
  " firstName, lastName) " +
  "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

values = [
  options.body.AccountHolderUserID, options.body.isSubUser,
  options.body.parentUsersID, options.body.appBrand,
  options.body.accessLevel, options.body.accessToken,
  options.body.tempPassword, options.body.email,
  options.body.firstName, options.body.lastName
];

pool.query(sql, values, ......); // note: just values, no extra square brackets

Upvotes: 0

Related Questions