Solved Function doesn't work from imported js-file, but work from within qml-file
-
Here is a function from within main.qml and it work as expected:
... TextField { id: textField1 placeholderText: qsTr("Password") focus: true } Button { id: button1 text: qsTr("Press Me") anchors.horizontalCenter: parent.horizontalCenter anchors.topMargin: 90 onClicked: DB.addPassword(textField1.text) } } function addPassword(pass) { print("DB.addPassword(): " + pass) var db = LocalStorage.openDatabaseSync("QQmlExampleDB", "1.0", "The Example QML SQL!", 1000000) db.transaction( function(tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS passwords(pw TEXT)'); tx.executeSql('DELETE from passwords'); tx.executeSql('INSERT into passwords VALUES(?)', [ pass ]); var rs = tx.executeSql('SELECT * FROM passwords') var r = rs.rows.item(0).pw console.log("addPassword: " + r) })
But if I move it to separate file, db.js it doesn't work anymore
function addPassword(pass) { print("DB.addPassword(): " + pass) db.transaction( function(tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS passwords(pw TEXT)'); tx.executeSql('DELETE from passwords'); tx.executeSql('INSERT into passwords VALUES(?)', [ pass ]); var rs = tx.executeSql('SELECT * FROM passwords') var r = rs.rows.item(0).pw console.log("addPassword: " + r) }) }
console.log("addPassword: " + r) tell me something "undefinded":
qml: DB.addPassword(): testtesttest qml: addPassword: undefined
What I'm doing wrong?
Thanks! -
Do you include js file correctly?
-
Here both files.
main.qml:import QtQuick 2.7 import QtQuick.Controls 2.0 import "DB.js" as DB //import QtQuick.LocalStorage 2.0 ApplicationWindow { visible: true width: 640 height: 480 title: qsTr("PassCheck") Column { anchors.horizontalCenter: parent.horizontalCenter anchors.topMargin: 70 anchors.top: parent.top spacing: 50 TextField { id: textField1 placeholderText: qsTr("Password") focus: true } Button { id: button1 text: qsTr("Press Me") anchors.horizontalCenter: parent.horizontalCenter anchors.topMargin: 90 onClicked: DB.addPassword(textField1.text) } } /* function addPassword(pass) { print("DB.addPassword: " + pass) var db = LocalStorage.openDatabaseSync("QQmlExampleDB", "1.0", "The Example QML SQL!", 1000000) db.transaction( function(tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS passwords(pw TEXT)'); tx.executeSql('DELETE from passwords'); tx.executeSql('INSERT into passwords VALUES(?)', [ pass ]); var rs = tx.executeSql('SELECT * FROM passwords') var r = rs.rows.item(0).pw console.log("R: " + r) }) } */ Component.onCompleted: DB.openDB() }
and DB.js
.pragma library .import QtQuick.LocalStorage 2.0 as Sql var db function openDB() { print("DB.openDB()") db = Sql.LocalStorage.openDatabaseSync("CheckPassDB","1.0","The CheckPass Database", 1000) createPassTable(); } function createPassTable() { print("DB.createPassTable()") db.transaction( function(tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS passwords(pw TEXT)'); }) } function addPassword(pass) { print("DB.addPassword(): " + pass) db.transaction( function(tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS passwords(pw TEXT)'); tx.executeSql('DELETE from passwords'); tx.executeSql('INSERT into passwords VALUES(?)', [ pass ]); var rs = tx.executeSql('SELECT * FROM passwords') var r = rs.rows.item(0).pw console.log("addPassword: " + r) }) }
-
Does your DB.js and main.qml are in the same folder?
-
Yep, they're in the same folder
-
Problem was solved by removing ~/.local/share/checkpass catalog (Linux)