反应本机领域对象

时间:2019-07-20 00:43:02

标签: javascript react-native realm react-native-android

我想知道为什么将Realm用于本机应用程序时得到以下信息。 {“ 0”:{“ id”:1,“ transactionType”:“收入”,“类别”:“ Paycheck”,“ amount”:“ 20.20”,“ account1”:“ Savings”,“ account2”:null} }

const Realm = require("realm")

const TransactionSchema = {
    name: "Transaction",
    primaryKey: "id",
    properties: {
        id: "int",
        //date: "date",
        transactionType: "string",
        category: "string",
        amount: "float",
        account1: "string",
        //account2 needed for transfer
        account2: { type: "string", optional: true }
    }
}


let test = new Realm({
    schema: [TransactionSchema]
})

export default class Test extends Component {
    constructor() {
        super()

        // this.state = {
        //  realm: null
        // }
    }

    saveTransaction() {
        test.write(() => {
            test.create(
                "Transaction",
                {
                    id: 1,
                    transactionType: "Income",
                    category: "Paycheck",
                    amount: 20.2,
                    account1: "Savings"
                },
                true
            )
        })
    }

getTransaction() {
        if (test !== null) {
            return test.objects("Transaction")
        } else {
            return "empty"
        }
    }

    render() {
        const info = this.getTransaction()
        if (info != null) {
            //const id = info["0"]["id"]
        }
        let myJSON = JSON.stringify(info)

        return (
            <View>
                <Button onPress={() => this.saveTransaction()} title="Save" />
                <Text>{JSON.stringify(info)}</Text>
                <Text>{myJSON}</Text>
            </View>
        )
    }

我很好奇为什么前面加了“ 0”。我期望{“ id”:1,“ transactionType”:“收入”,“类别”:“薪水”,“金额”:“ 20.20”,“ account1”:“储蓄”,“ account2”:null}

0 个答案:

没有答案