简体   繁体   English

每当我尝试访问 record.username 时,我都会收到“TypeError: Cannot read property 'username' of undefined”

[英]I am getting "TypeError: Cannot read property 'username' of undefined" whenever I try to access record.username

{record.userId.username} it says userId bascially doesnt have uesrname even though I referenced a route "USER" in it which originally contains "username": MAKECOMMENT FUNCTION: {record.userId.username}它说 userId 基本上没有 uesrname,即使我在其中引用了最初包含“username”的路由“USER”:MAKECOMMENT FUNCTION:

const makeComment = async (text,postId)=>{
    await fetch('/posts/' + post._id + '/comment',{
        method:"put",
        headers:{
            "Content-Type":"application/json",
        },
        body:JSON.stringify({
            postId,
            text
        })
    }).then(res=>res.json())
    .then(result=>{
        console.log(result)
        const newData = data.map(item=>{
          if(item._id==result._id){
              return result
          }else{
              return item
          }
       })
      setData(newData)
    }).catch(err=>{
        console.log(err)
    })
}

PORTION WHERE I ADD in javascript for diplay on application.我在 javascript 中添加的部分用于显示应用程序。 This is where the problem seems to occur(I figured out using trial and error).这就是问题似乎发生的地方(我通过反复试验发现)。 {record.userId.username} it says userId bascially doesnt have uesrname even though I referenced a route "USER" in it which originally contains "username": {record.userId.username}它说 userId 基本上没有 uesrname,即使我在其中引用了最初包含“username”的路由“USER”:

{
             post.comments.map(record=>{
              return(
                <h6 key={record._id} className="textInput"><span style={{fontWeight:"500"}}>{record.userId.username}</span> {record.text}</h6>
                )
              })
          }
          </div>
          <form onSubmit={(e)=>{
              e.preventDefault()
              makeComment(e.target[0].value, post._id)
          }}>
            <input type="text" placeholder="add a comment"/>  
          </form>

You need to check if your variable has values before displaying it.您需要在显示变量之前检查变量是否具有值。 Try this:尝试这个:

post ? post.comments.map(record=>{
              return(
                <h6 key={record._id} className="textInput"><span style={{fontWeight:"500"}}>{record.userId.username}</span> {record.text}</h6>
                )
              }) : <></>

if your variable has any values, it will continue to map.如果您的变量有任何值,它将继续到 map。

暂无
暂无

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

相关问题 我收到错误“无法读取未定义的属性“用户名”” - I am getting the error "cannot read property "username" of undefined" TypeError:无法读取password.js中未定义的属性“用户名” - TypeError: Cannot read property 'username' of undefined in passportjs 未捕获的类型错误:无法读取未定义的属性“用户名” - Uncaught TypeError: Cannot read property 'username' of undefined “ typeError:无法读取未定义的属性“用户名”。” - “typeError: Cannot read property 'username' of undefined.” 类型错误:无法读取未定义的属性“用户名”-- - TypeError: Cannot read property 'username' of undefined-- TypeError:无法读取未定义的属性“用户名”? - TypeError: Cannot read property 'username' of undefined? 为什么我收到“ TypeError:无法读取未定义的属性” length” - Why am I getting 'TypeError: Cannot read property 'length' of undefined' 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined 尝试在“ guildMemberAdd”中获取用户名时出现“ TypeError:无法读取未定义的属性&#39;username&#39;” - Getting “TypeError: Cannot read property 'username' of undefined” when trying to get the username of a user in “guildMemberAdd” 使用节点 JS 向 MongoDB 添加数据,我收到此错误:“TypeError: Cannot read property 'username' of undefined” - Adding data to MongoDB using node JS and I get this error: “TypeError: Cannot read property 'username' of undefined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM