Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

insert blob in startNewTransaction return 'invalid BLOB ID' #134

Open
sqllyw opened this issue Dec 7, 2022 · 0 comments
Open

insert blob in startNewTransaction return 'invalid BLOB ID' #134

sqllyw opened this issue Dec 7, 2022 · 0 comments

Comments

@sqllyw
Copy link

sqllyw commented Dec 7, 2022

Hi,

Following this link, I came out following working code:

 append_item_photo(itemno: number, caption: string, filename: string) {

        return new Promise((resolve, reject) => {

            try {

                let sql = `insert into picture(PICTNO, CAPTION, ITEMNO, JPEG) VALUES(GEN_ID(PICTNO_GEN, 1 ), '${caption}', ${itemno}, ? )`
                var stmt = this.con.prepareSync(sql);


                var blob = this.con.newBlobSync();
                var strm = new fb.Stream(blob);
                var fstrm = fs.createReadStream(filename);

                fstrm.resume();
                fstrm.pipe(strm);

                let cn = this.con

                strm.on("close", function () {
                    stmt.execSync(blob)
                    cn.commitSync();
                    resolve(true)
                    //    cb();
                });
                strm.on('error', function (err) {
                    console.log('error in write blob stream ', err);
                })
            } catch (error) {
                resolve(false)
            }
        })
    }

Now, I'd like to do this in a specific transaction, following is the code, but got 'invalid BLOB ID':

append_item_photo(itemno: number, caption: string, filename: string) {

        return new Promise((resolve, reject) => {

            this.con.startNewTransaction(async (err, tx) => {

                try {

                    let sql = `insert into picture(PICTNO, CAPTION, ITEMNO, JPEG) VALUES(GEN_ID(PICTNO_GEN, 1 ), '${caption}', ${itemno}, ? )`
                    var stmt = tx.prepareSync(sql);


                    var blob = this.con.newBlobSync();  // this uses default transaction?
                    var strm = new fb.Stream(blob);
                    var fstrm = fs.createReadStream(filename);

                    fstrm.resume();
                    fstrm.pipe(strm);

                    strm.on("close", function () {
                        stmt.execInTransSync(tx, blob)
                        tx.commitSync();
                        resolve(true)
                    });
                    strm.on('error', function (err) {
                        console.log('error in write blob stream ', err);
                    })


                } catch (error) {
                    resolve(false)
                }
            });

        })

    }

looks like :

var blob = this.con.newBlobSync();

it should have been

    var blob = tx.newBlobSync();

but Transaction does not have newBlobSync()

any idea how to do it? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant