0

I'm trying to read information from a database of U1db.
I encountered this error
'ReferenceError: contents is not defined'
Here's my code:

    import U1db 1.0 as U1db
    U1db.Database {
            id: db
            path: "lU1DbDatabase"
        }
    U1db.Document {
            id: doc
            database: db
            docId: 'doc1'
            create: true
            defaults: { "name":["Jerusalem"]}
        }
    U1db.Index{
            database: db
            id: index1
            expression: [["name"]]
        }
    U1db.Query{
            id: query1
            index: index1
        }
     OptionSelector {
                id:selector1
                model:query1
                delegate: OptionSelectorDelegate {
                                    text: i18n.tr(contents.name)//this work perfect
                                }
                onSelectedIndexChanged: {
                    console.log(contents.name);//this get the error!
                }
   }

this is part of large code that i cut the part that cause the problem.
what am i doing wrong? thank you!

1

Your problem is not with U1DB, but with the OptionSelector. Let's consider a simpler example:

import QtQuick 2.0
import Ubuntu.Components 1.0

MainView {
    OptionSelector {
        model: ListModel {
            ListElement { name: "Item 1" }
            ListElement { name: "Item 2" }
        }
        delegate: OptionSelectorDelegate {
            text: i18n.tr(name)
        }
        onSelectedIndexChanged: {
            console.log(name)
        }
    }
}

When the index is changed, we get the error, "ReferenceError: name is not defined". And why would we expect it to be defined? We've defined no variable named "name", and the OptionSelector doesn't have a "name" property.

Why can we reference "name" in the delegate? A new object is created from the delegate for each element in the model. This object gets the properties from that list element, and our list elements contain a "name" property. Similarly, in your example, the U1db.Query produced list elements with a "contents" property, which evidently was an object with a "name" property.

If we want to reference those properties outside of the delegate, we need to reach into the model and extract them:

            console.log(model.get(selectedIndex).name)

In your case, the model is a U1db.Query, not a ListModel, so it doesn't have a get() method. It does have a results list that I presume you can index, but I don't know enough about U1db to say more. Hopefully this is enough to get you on the right path.

2
  • Thank you for your answer, this line has solved the problem for me: var doc = loacationDatabase.getDoc(model.documents[selectedIndex]); it stores the whole document into a variable and then I can read all the data. i think this is the best solution that i have. May 28 '15 at 5:38
  • or just model.results[selectedIndex].name[0] May 28 '15 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.