在尝试加载一个呈现从外部API提取的产品信息的页面时,看到上面的错误。错误仅发生在生产环境中,并且本地一切运行正常。检查控制台时,我得到此错误:
TypeError: undefined is not a function (near '...n.map...')
“Next Build”工作正常,生成了一个生产版本。“Next Start”也工作正常,在控制台或其他任何地方都没有抛出错误。错误只在我推到生产环境后发生。我对发生了什么感到困惑。我的代码如下:
呈现产品列表的组件:
import type { NextPage } from 'next'
import React, { useState, useEffect, useContext} from 'react';
import styles from '../styles/Home.module.css'
import DepopIcon from './icons/depop.svg'
import { Image, Main, Heading, Paragraph, Nav, Icons, Header, Grommet, Box, Button, Grid, Text, Footer, Anchor } from 'grommet';
import {
Instagram,
Shop,
Cart
} from 'grommet-icons';
import { useRouter } from 'next/router';
export default function ProductsList() {
const router = useRouter();
// load our products state variable
const [products, setProducts] = useState([]);
const [prodImg, setProdImg] = useState([]);
function handleProdClick(id){
router.push({ pathname: "/product", query: { id: id } })
}
const getProducts = async () => {
const response = await fetch("/api/getproducts");
var data = await response.json();
data = JSON.parse(data);
await setProducts(data);
}
useEffect(() => {
getProducts();
}, [])
//
// build areas list for our grid
const areaVar = "area";
const cols = 3;
var r = 0;
var c = 0;
var areas = []
for (let x = 1; x < products.length+1; x++){
const name = areaVar+x.toString()
areas.push({ name: name, start: [r,c], end: [r,c] })
r += 1
if (x % 3 == 0){
r = 0
c += 1
}
}
//
console.log(products);
// create our product objects for our grid
var productObjs = [];
if (typeof products != "undefined" && products != null){
productObjs = products.map((product, index) => {
return(
<Box
key={product._id}
gridArea= {areas[index].name}
background="#003311"
direction="column"
pad="none"
align="center"
onClick={() => {handleProdClick(product._id)}}
>
<Image
src={product.frontImage} fill={true} fit="cover"
onMouseOver={e => (e.currentTarget.src = product.backImage)}
onMouseOut={e => (e.currentTarget.src = product.frontImage)}
/>
<Box
direction="row"
pad="xsmall"
align="center"
gap="large"
justify="between"
animation="fadeIn"
>
<Text color="#fff" size="medium">{product.name}</Text>
<Text color="#fff" size="medium">{product.color}</Text>
<Text color="#fff" size="medium">{product.price} USD</Text>
<Text color="#fff" size="medium">{product.size}</Text>
</Box>
</Box>
);
});
}
//
const rows = []
for (let i = 0; i < r; i++){
rows.push('large')
}
return (
<Grid
rows={rows}
columns={['flex', 'flex', 'flex']}
gap="none"
areas={areas}
>
{productObjs}
</Grid>
);
}
使用上述组件的页:
import type { NextPage } from 'next'
import React, { useState, useContext, useEffect} from 'react';
import { Paragraph, Main, Nav, Icons, Header, Grommet, Box, Button, Grid, Text, Footer, Anchor } from 'grommet';
import ContactHeaderUnit from '../components/ContactHeaderUnit.tsx'
import FooterUnit from '../components/FooterUnit.tsx'
import ProductsList from '../components/ProductsList.tsx'
const Shop: NextPage = () => {
return (
<Grommet
full={true}
background="#fffced"
theme={{
global: { font: {
family: "Alice"
}},
paragraph: {
font: {
family: 'Arial',
},
},
label: {
font: {
family: 'Arial',
},
},
}}
>
<ContactHeaderUnit/>
<ProductsList/>
<FooterUnit/>
</Grommet>
)
}
export default Shop;
3条答案
按热度按时间ni65a41a1#
我遇到了这个错误,尽管我在构建时没有遇到任何错误,这是非常困难的弄清楚花了几天时间。原来我的一些传入数据被渲染成一些无效的html(动态创建的脚本标记出了问题)。我能够通过反复试验来解决这个错误,删除不同的传入数据片段,看看我的页面是否会呈现。如果我必须再做一次的话,我会从那里开始。
3zwtqj6y2#
我有这个错误,这是什么帮助我在我的项目。
来自我的暂存站点(Heroku)的屏幕,(在本地没有错误)
在我的本地(开发模式)上,我已经将API更改为从临时站点获取,而不是本地(我使用Strapi CMS)
我在本地的错误,给了我更详细的错误,在我的案例中链接
为了找到错误的来源,我在Layout.js文件中一个一个地取消了注解,1)页眉,2)主,3)页脚,是页脚产生了错误,并修复了页脚,没有更多的错误
eqoofvh93#
你可以在移动的上用chrome://inspect检查控制台,帮我找出问题