NodeJS Mongoose record.save TypeError
Posted
技术标签:
【中文标题】NodeJS Mongoose record.save TypeError【英文标题】: 【发布时间】:2018-03-28 08:27:24 【问题描述】:我正在使用 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)
非常感谢任何想法。
【问题讨论】:
req.originalUrl
的值是多少?您的查询似乎包含不正确的值。
【参考方案1】:
您可能传递的是 JSON 字符串而不是 JSON 对象,您需要在创建记录之前将其解析为 JSON 对象
替换
var record = new Iptv(query);
与
var record = new Iptv(JSON.parse(query));
【讨论】:
【参考方案2】:大多数浏览器都支持 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);
【讨论】:
以上是关于NodeJS Mongoose record.save TypeError的主要内容,如果未能解决你的问题,请参考以下文章