React Native - Invariant Violation:对象作为React Child无效(使用Node JS作为后端)

问题描述 投票:1回答:2

在这里,我试图获得订单的总和以及我在后端使用Node JS的数量之和。问题是每当我运行我的代码时 - 我的提取函数似乎无法正常工作或者我错过了一些我不知道的东西。

但是使用postman,我的API正在使用预期的输出。如果我在反应本机代码中使用Buuut它会显示一些错误。

Sum using postman Quantity using postman

这是我的后端代码:

OrderNo.js(模型)//后端

var Task = {
    Sum:function(id,callback) {
        return db.query("SELECT SUM(order_amount) AS TotalAmountOrdered FROM orders where order_no=?",[id],callback);
    },
}

OrderNo.js(路由器)//后端

var Task = require('../models/OrderNo');
router.get('/ForSum/:id?', (req, res, next) => {
    Task.Sum(req.params.id,function(err,rows) {
        if(err) {
            res.json(err);
        }
        else {
            res.json(rows);
        }
    });
});

NumOrder.js(模型)//后端

var Task = {
    NumOrder:function(id,callback) {
        return db.query("SELECT SUM(order_quantity) AS TotalItemsOrdered FROM orders where order_no=?",[id],callback);
    },
}

NumOrder.js(路由器)//后端

var Task = require('../models/NumOrder');
router.get('/num/:id?', (req, res, next) => {
    Task.NumOrder(req.params.id,function(err,rows) {
        if(err) {
            res.json(err);
        }
        else {
            res.json(rows);
        }
    });
});

这是我的React-Native代码

export default class Settlement extends Component {
    constructor(props){
    super(props)
        this.state = {
            orderDet: this.props.navigation.state.params.orderDet,
            numOrder: [],
            TotalSum: [],
        };
    }
    fetchSum = async () => {
        const response = await fetch("http://192.168.254.104:3308/OrderNo/ForSum/" + this.state.orderDet)
        const json = await response.json()
        this.setState({ TotalSum: json })
    }
    fetchNumOrders = async () => {
        const response = await fetch("http://192.168.254.104:3308/NumOrder/num/" + this.state.orderDet )
        const json = await response.json()
        this.setState({ numOrder: json })
    }
    componentDidMount() {
        this.fetchNumOrders();
        this.fetchSum();
    }
    render() {
        return (
            <View>
                <Text>Number of Orders: { this.state.numOrder }</Text>
                <Text>Total Amount: ₱{ this.state.TotalSum }</Text>
            </View>
        )
    }
}

This is the error for Sum This is the error for Quantity

这是我的DB

** PS:**我也在我的每个获取函数上尝试了“json [0] .order_no”并且没有错误,但我的输出是空的。

enter image description here

DB

javascript android node.js reactjs react-native
2个回答
0
投票

根据Postman中的响应对象,您需要执行以下操作

this.state = {
        orderDet: this.props.navigation.state.params.orderDet,
        numOrder: null,
        TotalSum: null,
};

fetchSum = async () => {
        const response = await fetch("http://192.168.254.104:3308/OrderNo/ForSum/" + this.state.orderDet)
        const json = await response.json()
        this.setState({ TotalSum: json[0].TotalAmountOrdered })
    }

fetchNumOrders = async () => {
        const response = await fetch("http://192.168.254.104:3308/NumOrder/num/" + this.state.orderDet )
        const json = await response.json()
        this.setState({ numOrder: json[0].TotalItemsOrdered })
    }

0
投票

该错误意味着您不能将对象或数组作为子组件,即<Text>。您只能在组件内显示字符串或数字。

<Text>Number of Orders: { this.state.numOrder[0].TotalAmountOrdered }</Text>// Inside {} value of variable should be string or number not array or object

错误是您将this.state.numOrder的值设置为数组

© www.soinside.com 2019 - 2024. All rights reserved.