typeorm icon indicating copy to clipboard operation
typeorm copied to clipboard

Simple-array column type in typeorm + mysql returns a null value instead of the array unexpectedly

Open MohammedAK1991 opened this issue 3 years ago • 0 comments

Issue Description

Im trying to add an item to a simple-array type column to a mysql table through typeorm. It works for most part, but sometimes typeorm returns null for the value of the column which is super weird

Expected Behavior

  • The column should return the array of values and not null

Actual Behavior

  • I tried every method in typeorm : using entity manager, repository api and even a raw SQL query, but still sometimes typeorm returns a null value instead of the actual array 5 times out of 10. I know typeorm under the hood concatenates the array into a string but it clearly doesnt work

Steps to Reproduce

const identifyTest = functions
    .region("europe-west2")
    .https.onRequest(async (req, res) => {
      cors()(req, res, async () => {
      // Set CORS headers for preflight requests
      // Allows POSTs from any origin with the Content-Type header
      // and caches preflight response for 3600s

        res.set("Access-Control-Allow-Origin", "*");

        if (req.method === "OPTIONS") {
        // Send response to OPTIONS requests
          res.set("Access-Control-Allow-Methods", "POST");
          res.set("Access-Control-Allow-Headers", "Content-Type");
          res.set("Access-Control-Max-Age", "3600");
          res.status(204).send("");
        } else {
          const {
            uid,
            email,
            revenue,
            country,
            qoins_amount,
            qoala_code,
            test_number,
            initial_utm_campaign,
            initial_utm_medium,
            initial_utm_source,
            initial_utm_content,
            initial_gclid,
            referral_id,
            purchase_count,
            first_transaction_timestamp,
            last_transaction_timestamp,
          } = req.body;

          const anonymous_id = req.body.anonymous_id || "";

          try {
            const userPayload = {
              uid,
              anonymous_id,
              email,
              revenue,
              country,
              qoins_amount,
              qoala_code,
              test_number,
              initial_utm_campaign,
              initial_utm_medium,
              initial_utm_source,
              initial_utm_content,
              initial_gclid,
              referral_id,
              purchase_count,
              first_transaction_timestamp,
              last_transaction_timestamp,
            };


            const {error} = UserSchema.validate(userPayload);

            if (error) {
              res
                  .status(400)
                  .send(OperationResult.error("Joi Schema validation failed " +
                  error));
              console.error("Joi schema not validated " + userPayload + error );
              logger.error("Joi schema not validated " + userPayload + error );
            }
            // we initialize  new connection to cloudSQL if it isnt already open
            if (!dataSource.isInitialized) {
              await dataSource.initialize();
            }

            // this makes sure the instance and local data is always in sync
            await dataSource.synchronize();

            const userRepository = dataSource.getRepository(User);

            const userToUpdate = await dataSource
                .getRepository(User)
                .createQueryBuilder("user")
                .where("user.uid = :uid", {uid: req.body.uid})
                .getOne();

    
            let result;

            if (userToUpdate) {
              console.log("exists");
              const currentAnonymous_idArray = userToUpdate.anonymous_id || [];
              userToUpdate.anonymous_id = [anonymous_id, ...currentAnonymous_idArray];
              result = await userRepository.save(userToUpdate);
            } else {
              console.log("new user");
              userPayload.anonymous_id = [anonymous_id];
              result = await userRepository.save(userPayload);
            }

            res.status(200).send(OperationResult.success(result));
          } catch (err) {
            res.status(500).send(OperationResult.error(err));
            console.error(
                "error updating user row in cloudSQL",
                req.body,
                err,
            );
            logger.error(
                "error updating user row in cloudSQL",
                req.body,
                err,
            );
          }
        }
      });
    });

export default identifyTest;

My Environment

Dependency Version
Operating System MacOs 11.3.1 (20E241)
Node.js version v16.13.0
Typescript version [email protected]
TypeORM version [email protected]

Additional Context

Need this resolved asap please. My jobs depends on this

Relevant Database Driver(s)

DB Type Reproducible
aurora-mysql no
aurora-postgres no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql yes
nativescript no
oracle no
postgres no
react-native no
sap no
spanner no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✅ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

MohammedAK1991 avatar Sep 21 '22 10:09 MohammedAK1991