Javascript 如何处理此类型错误:无法读取属性';uri';未定义的

Javascript 如何处理此类型错误:无法读取属性';uri';未定义的,javascript,node.js,class,express,asynchronous,Javascript,Node.js,Class,Express,Asynchronous,你好,house,我在常量uri=的第8行代码中有以下错误。。。 第8行中声明的uri常量似乎抛出了错误,但我不知道它是什么 (节点:11524)未处理的PromisejectionWarning:TypeError:无法读取未定义的属性“uri” 我已经包括了下面的代码。您的答复将不胜感激 完整代码 class URIGenerator { getURI(controllerAction, params, id) { const caArray = controllerActio

你好,house,我在常量uri=的第8行代码中有以下错误。。。 第8行中声明的uri常量似乎抛出了错误,但我不知道它是什么

(节点:11524)未处理的PromisejectionWarning:TypeError:无法读取未定义的属性“uri”

我已经包括了下面的代码。您的答复将不胜感激

完整代码


class URIGenerator {
  getURI(controllerAction, params, id) {
    const caArray = controllerAction.split('_');
    const routeData = RoutesCollection[caArray[0]][caArray[1]];
    const uri = params ? this._bindParams(routeData.uri, params) : routeData.uri; //line 8
    return {
      id: id || routeData.action,
      method: routeData.method,
      uri,
    };
  }

  _bindParams(uri, params) {
    let match;
    let replacement;
    let uriParam = uri;
    const replacedParams = [];

    while (match = /:([\w_]+)\??/ig.exec(uriParam)) {
      replacement = params[match[1]].toString() || '';
      if (replacement === '') {
        uriParam = uriParam.replace(`/${match[0]}`, '');
      } else {
        uriParam = uriParam.replace(match[0], replacement);
        replacedParams.push(match[1]);
      }
    }

    const paramsForQueryString = {};
    Object.keys(params).forEach((p) => {
      if (!replacedParams.includes(p)) {
        paramsForQueryString[p] = params[p];
      }
    });

    if (Object.keys(paramsForQueryString).length > 0) {
      uriParam = `${uriParam}?${queryString.stringify(paramsForQueryString)}`;
    }

    return uriParam;
  }
}

module.exports = URIGenerator;

错误代码实际上很清楚:

无法读取未定义的属性“uri”

您正在尝试访问两个都是“routeData.uri”的位置上的.uri属性

您将其定义为:


因此,RouteCollection可能是代码中未包含的问题。

也许您为
getURI
方法提供了不正确的参数,尤其是
controllerAction
sessioni会假定routeData.uri是错误的。您应该检查routeData对象是否具有属性uri。
const routeData = RoutesCollection[caArray[0]][caArray[1]];