我正在使用Express和Mongoose在NodeJS上构建一个小型REST API,并且我遇到了一个似乎无法解决的TypeError。
Express Router和Mongoose如下:
const Router = require('express').Router;
const router = new Router();
const querystring = require('querystring')
const mongoose = require('mongoose')
const Schema = mongoose.Schema;
const iptvSchema = new Schema({
appName: { type: String, required: true },
userId: { type: String, required: true },
level: { type: String, required: true },
title: { type: String, required: true },
message: {type: String},
meta: {
ip: {type: String},
mac: {type: String},
campus: {type: String},
venue: {type: String},
room: {type: String},
serial: {type: String}
},
tags: {type: Array}
});
const Iptv = mongoose.model('Iptv', iptvSchema)
router.route('/add/')
.all(function(req, res, next) {
console.log("/add/ route")
var query = Object.keys(querystring.parse(req.originalUrl))[1]
console.log(query)
var record = new Iptv(query);
record.save(function(err) {
console.log("save")
if (err) {
console.log("error")
console.log(err)
} else {
console.log("record created")
}
})
})
module.exports = router;
传入请求是来自客户端JQuery的GET AJAX请求。 query
变量被验证为从GET请求中提取的正确JSON字符串:
{"appName":"iptv","userId":"0002026941e2","level":"info","title":"Amino Connected"}
请求错误如下:
TypeError: Cannot use 'in' operator to search for '_id' in {"appName":"iptv","userId":"0002026941e2","level":"info","title":"Amino Connected"}
at model.Document.$__buildDoc (/Users/jeremylo/Documents/dev/log/node_modules/mongoose/lib/document.js:222:24)
at model.Document (/Users/jeremylo/Documents/dev/log/node_modules/mongoose/lib/document.js:67:20)
at model.Model (/Users/jeremylo/Documents/dev/log/node_modules/mongoose/lib/model.js:48:12)
at new model (/Users/jeremylo/Documents/dev/log/node_modules/mongoose/lib/model.js:3785:13)
at /Users/jeremylo/Documents/dev/log/model/iptv/router.js:31:18
at Layer.handle [as handle_request] (/Users/jeremylo/Documents/dev/log/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/jeremylo/Documents/dev/log/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/jeremylo/Documents/dev/log/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/jeremylo/Documents/dev/log/node_modules/express/lib/router/layer.js:95:5)
at /Users/jeremylo/Documents/dev/log/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/jeremylo/Documents/dev/log/node_modules/express/lib/router/index.js:335:12)
at next (/Users/jeremylo/Documents/dev/log/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/jeremylo/Documents/dev/log/node_modules/express/lib/router/index.js:174:3)
at router (/Users/jeremylo/Documents/dev/log/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/Users/jeremylo/Documents/dev/log/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/jeremylo/Documents/dev/log/node_modules/express/lib/router/index.js:317:13)
非常感谢任何想法。
答案 0 :(得分:1)
可能您传递的是JSON字符串而不是JSON对象,您需要在创建记录之前将其解析为JSON对象
替换
tabs
与
var record = new Iptv(query);
答案 1 :(得分:0)
大多数浏览器都支持JSON.parse(),它在ECMA-262第5版(JavaScript所基于的规范)中定义。它的用法很简单:
var json = '{"appName":"iptv","userId":"0002026941e2","level":"info","title":"Amino Connected"}',
obj = JSON.parse(json);
console.log(obj.appName);