我需要帮助保存Mongoose支持的模型的实例:保存实体会引发以下错误:
node.js:134
00:27:16 web.1 | throw e; // process.nextTick error, or 'error' event on first tick
00:27:16 web.1 | ^
00:27:16 web.1 | TypeError: Cannot call method 'decodeInt' of undefined
00:27:16 web.1 | at model.<anonymous> (.../node_modules/mongoose-types/lib/plugins/useTimestamps.js:13:37)
00:27:16 web.1 | at VirtualType.applyGetters (.../node_modules/mongoose/lib/virtualtype.js:53:25)
00:27:16 web.1 | at model.get (...
模型定义为
var mongooseTypes = require("mongoose-types"), useTimestamps = mongooseTypes.useTimestamps;
mongooseTypes.loadTypes(mongoose);
var Url = mongoose.SchemaTypes.Url;
var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
var TextSnippetSchema = new mongoose.Schema({
text_id : ObjectId
, text : String
, context : String
, url : Url
, position : String
});
TextSnippetSchema.plugin(useTimestamps);
var TextSnippet = mongoose.model('TextSnippet',TextSnippetSchema);
要创建的代码&保存一个新实体如下所示:
var instance = new TextSnippet();
instance.text= req.query["text"];
instance.context= req.query["ctx"];
instance.url = req.query["url"];
instance.position= "";
console.log(JSON.stringify(instance));
instance.save(function (err) {
console.log(err);
});
据我所知,我没有需要"decodeInt"的属性:然而,实体无法保存。
你能帮我让对象持久化为我工作吗?
问题是mongodb不再导出BinaryParser。您需要更改在useTimestamps中导入类似的内容。
文件./node_modules/mongoose types/lib/plugins/useTimestamps.js
var mongoose = require('mongoose')
, ObjectID = mongoose.ObjectID
, BinaryParser = require('mongoose/node_modules/mongodb/node_modules/bson').BinaryParser;
你应该很乐意去。我也会为此向所有者提交一个补丁。