Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

javascript - MySQL NodeJS not getting latest data after write

I am having trouble figuring out the problem to an issue where when I write data (create, update, delete) then write a query to get the data after, the data that I receive back is the data prior to the write.

For example:

Let's say I have two functions createApple() and getAppleById. I have a utility function called getConnection() that gets a connection from a pool to be used for transactional queries. I have an endpoint that creates an apple and I get back to the insertId from mysql then I use that to get the apple but when I return it as a response, I get an empty object.

const createApple = async ({ type }) => {
    const connection = await getConnection();
    await connection.beginTransaction();
    return await connection.query(`INSERT INTO apple (type) VALUES (?)`, [type]);
}

const getAppleById = async (appleId) => {
   const connection = await getConnection();
   return await connection.query(`SELECT * FROM apple WHERE id = ?`, [appleId]);
}
router.post(`/api/apples`, async (req, res) => {
    const { insertId: createdAppleId } = await createApple({ ...req.body });
    const apple = await getAppleById(createdAppleId);
    res.status(201).send(apple); // <-- this returns {}
});

I noticed that if I add a console.log() before sending the data back, it does get back the data, for example:

router.post(`/api/apples`, async (req, res) => {
    const { insertId: createdAppleId } = await createApple({ ...req.body });
    const apple = await getAppleById(createdAppleId);
    console.log(apple);
    res.status(201).send(apple); // <-- this now returns the newly created apple
});

Any ideas on why this may be happening? Also, is this considered a good way of getting a newly created/updated entity or would it be better to make two separate calls:

  1. First call to create/edit the entity (a POST or PATCH call)
  2. Second call to get the entity (a GET call)

Any help is appreciated!

Thanks!

question from:https://stackoverflow.com/questions/65928809/mysql-nodejs-not-getting-latest-data-after-write

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
const createApple = async ({ type }) => {
    const connection = await getConnection();
    await connection.beginTransaction();
    await connection.query(`INSERT INTO apple (type) VALUES (?)`, [type]);
    await connection.commit();
}

I think error this function when you use transaction, you should commit or rollback transaction after finish query

This is best practice for me, I hope it useful for you

const createApple = async ({ type }) => {
    const connection = await getConnection();
    await connection.beginTransaction();
    try{
        await connection.query(`INSERT INTO apple (type) VALUES (?)`, [type]);
        await connection.commit();
    }catch{
        await connection.rollback()
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...