简体   繁体   English

为什么 push 显示为未定义,即使它是一个关键字并且我没有将其声明为任何 object?

[英]Why is push shown as undefined, even though it is a keyword and i have not declared this as any object?

function updateRecords(id, prop, value) {
  if (prop !== "tracks" && value !== ""){
    collection[id][prop] = value;
  }


  if (collection[id].hasOwnProperty("tracks") == true){
    if (value === ""){
      delete collection[id].prop;
    }
    else{
      collection[id].tracks.push(value);
    }
  }

  if (prop === "tracks" && collection[id].hasOwnProperty(prop) == false){
    collection.prop = [];
    collection[id].prop.push(value);


  }



  return collection;
}

So the function should add new values to the arrays, but I am not sure why i keep getting an error saying it is push of undefined.所以 function 应该向 arrays 添加新值,但我不知道为什么我一直收到一个错误,说它是未定义的推送。

It seems like you are trying to push something to collection[id].prop ?似乎您正在尝试将某些内容推送到collection[id].prop

You must have left out the index reference for collection at the previous line:您必须在上一行遗漏了collection的索引参考:

collection[id].prop = [];

暂无
暂无

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

相关问题 为什么即使声明的变量中没有“ var”关键字,我仍会收到“ Uncaught ReferenceError”? - why do i get “Uncaught ReferenceError” even though variable is declared without “var” keyword? 即使我声明了会话,Express-session也会返回“ undefined” - Express-session returns 'undefined', even though I declared the session 为什么即使我已将数字转换回字符串,我也不能将数字推送到我的字符串数组中? - Why can I not push a number into my array of strings even though I have converted the number back to a string? 即使声明了变量,也出现未定义的错误 - Undefined error even though variable is declared 为什么即使我将create标志设置为true,getFile()也不创建任何文件? - Why isn't getFile() creating any files even though i have set the create flag on true? 无法读取推送未定义的属性? 即使我将评论推送到评论数组,为什么我会收到此错误? - Cannot read property of push undefined? Even though I am pushing comment to comments array why am I receiving this error? 即使值存在,为什么对象键返回 undefined - Why object key returns undefined even though value exists 即使我没有将任何字段设置为唯一,为什么 Mongo 会给我 E11000 错误? - Why is Mongo giving me the E11000 error even though I don't have any field set to unique? 即使属性存在,js对象也返回undefined - js object returns undefined even though property is there 即使已初始化,对象变量也未定义 - Object variable is undefined even though it's initialised
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM