繁体   English   中英

列表中的每个孩子都应该有一个独特的“关键”道具。 但我没有调用任何组件

[英]Each child in a list should have a unique "key" prop. but i'm not calling any component

如果我使用 map 方法调用所有折扣价。 它完美地工作,但我在控制台中看到并且发现了这个错误。 如何修复它

 <div className="priceCal flex flex-col items-center">
       <p style={{ fontSize: '1.5em' }} className='font-medium pt-3'>Total items: </p>
          {
            data.map((v, i)=>{
            return <p className='font-bold text-xl'>Rs. {v.DiscountPrice}</p>
             })
         }
</div>

点击查看错误

react-jsx-dev-runtime.development.js:117 Warning: Each child in a list should have a unique "key" prop.

Check the render method of `Cart`. See https://reactjs.org/link/warning-keys for more information.
    at p
    at Cart (http://localhost:3000/static/js/bundle.js:3393:74)
    at Routes (http://localhost:3000/static/js/bundle.js:63733:5)
    at RouterMenu
    at App (http://localhost:3000/static/js/bundle.js:140:81)
    at Router (http://localhost:3000/static/js/bundle.js:63666:15)
    at BrowserRouter (http://localhost:3000/static/js/bundle.js:63146:5)

您需要像这样向返回的父元素添加一个键

return <p className='font-bold text-xl' key={i}>Rs. {v.DiscountPrice}</p>

但如果id存在,最好使用 object 的独特属性,例如key={v.id}

你正在循环

data.map((v, i)=>{
        return <p className='font-bold text-xl'>Rs. {v.DiscountPrice}</p>
})

只需在返回 JSX 中添加一个键

data.map((v, i)=>{
        return <p key={index} className='font-bold text-xl'>Rs. {v.DiscountPrice}</p>
})

如果您在 v object 中有唯一标识符,请使用该标识符

参考 => https://reactjs.org/docs/lists-and-keys.html#keys

I want code and API to fetch data from database buy passing query for one specific id. I tried the code but the code is throwing error. I have already connected database and I have already coded an API to fetch all the data from the database.

下面我附上了我的代码

  app.get('/:Employee_id', (req,res) => {
            pool.getConnection((err, connection) =>{
                if(err) throw err;
                console.log('connected as id' + connection.threadId);
                
                connection.query('Select* from employee where Employee_id = ?', [req.params.Employee_id], (err, rows)=>{
                    connection.release();
                    
                    if(!err){
                        res.send(`employee with the id: ${Employee_id}`);
                    }
                    else{
                        console.log(err);
                    }
                });
            });
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM