javascript - 当我尝试运行 mt Node.js 应用程序时出现 TypeError

标签 javascript node.js rest api typeerror

我正在尝试从 REST API Development With Node.jsworking 获取示例程序。当我尝试运行它时,出现此错误:

TypeError: Cannot read property 'indexOf' of undefined
    at translateTypeToJs (/Users/paulcarron/Documents/Books/REST API Development With Node.js/lib/db.js:93:8)
    at translateComplexType (/Users/paulcarron/Documents/Books/REST API Development With Node.js/lib/db.js:58:21)
    at _.each (/Users/paulcarron/Documents/Books/REST API Development With Node.js/lib/db.js:83:13)
    at Function._.each._.forEach (/Users/paulcarron/Documents/Books/REST API Development With Node.js/node_modules/underscore/underscore.js:191:9)
    at Object.getModelFromSchema (/Users/paulcarron/Documents/Books/REST API Development With Node.js/lib/db.js:76:5)
    at module.exports (/Users/paulcarron/Documents/Books/REST API Development With Node.js/models/book.js:7:21)
    at module.exports (/Users/paulcarron/Documents/Books/REST API Development With Node.js/models/index.js:3:30)
    at Object.<anonymous> (/Users/paulcarron/Documents/Books/REST API Development With Node.js/lib/db.js:21:35)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)

我已经浏览了代码,甚至检查了 GitHub project但看不出我做错了什么。从上面的日志来看,它似乎是从默认情况下调用 translateTypeToJs 。我查了一下书上的内容,确实是这样。我该如何解决这个问题?

这是我的 lib/db.js 代码:

const config = require("config"),
  _ = require("underscore"),
  mongoose = require("mongoose"),

  Schema = mongoose.Schema;

let obj = {
  cachesModels: {},
  getModelFromSchema: getModelFromSchema,
  model: function(mname) {
    return this.models[mname]
  },
  connect: function(cb) {
    mongoose.connect(config.database.host + "/" + config.database.dbname);
    this.connection = mongoose.connection;
    this.connection.on('error', cb);
    this.connection.on('open', cb);
  }
}

obj.models = require("../models/")(obj);

module.exports = obj;

function translateComplexType(v, strType) {
  let tmp = null;
  let type = strType || v['type'];
  switch(type) {
    case 'array':
      tmp = [];
      if(v['items']['$ref'] != null) {
        tmp.push({
          type: Schema.ObjectId,
          ref: v['items']['$ref']
        });
      } else {
        let originalType = v['items']['type'];
        v['items']['type'] = translateTypeToJs(v['items']['type']);
        tmp.push(translateComplexType(v['items'], originalType));
      }
    break;
    case 'object':
      tmp = {};
      let props = v['properties'];
      _.each(props, (data, k) => {
        if(data['$ref'] != null) {
          tmp[k] = {
            type: Schema.ObjectId,
            ref: data['$ref']
          }
        } else {
          tmp[k] = translateTypeToJs(data['type']);
        }
      });
    break;
    default:
      tmp = v;
      tmp['type'] = translateTypeToJs(type);
    break;
  }
  return tmp;
}


/**
 * Turns the JSON Schema into a Mongoose schema
 */
function getModelFromSchema(schema){
  let data = {
    name: schema.id,
    schema: {}
  }

  let newSchema = {};
  let tmp = null;
  _.each(schema.properties, (v, propName) => {
    if(v['$ref'] != null) {
      tmp = {
        type: Schema.Types.ObjectId,
        ref: v['$ref']
      }
    } else {
      tmp = translateComplexType(v) //{}
    }
    newSchema[propName] = tmp;
  });
  data.schema = new Schema(newSchema);
  return data;
}


function translateTypeToJs(t) {
  if(t.indexOf('int') === 0) {
    t = "number";
  }
  return eval(t.charAt(0).toUpperCase() + t.substr(1));
}

最佳答案

如果 translateComplexType(v)//{} 行注释的含义是 v 可以是该行上的空对象,则:

  • let type = strType || v['type'] 可以使 type === undefined
  • translateTypeToJs(undefined) 将对 tmp['type'] = translateTypeToJs(type) 进行调用(从而引发 TypeError)

我建议进行以下修复:

let type = strType || v['type'] || 'undefined'

关于javascript - 当我尝试运行 mt Node.js 应用程序时出现 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51910979/

相关文章:

node.js - 在node.js中使用zlib使用字典压缩数据

java - GET 请求被禁止,而 POST 请求则正常

java - Java 中的 RESTful Web 服务 - 400 错误请求 - 方法调用错误?

java - 在 RESTful API 和 WSO2 的背景下,Apache Tomcat 和 Apache Axis2 之间的关系是什么

javascript - 填充后获取输入变量然后执行ajax

javascript - Angular 组织和模块化 - Controller 结构

重定向手机用户的Javascript代码

c# - 将代码写入多行

node.js - 为什么使用 Redis 而不是存储在普通变量中?

node.js - 无法在 ubuntu 上安装 npm bcrypt