我正在尝试使用Node.js从REST API开发中获取示例程序。当我尝试运行它时,出现此错误:
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'