这是模型
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var PostSchema = new Schema({
post_author: { type: Schema.ObjectId, ref: "User" },
post_text: String,
total_comments: { type: Number, 'default': 0 },
total_likes: { type: Number, 'default': 0 },
likes_users: [{ type: Schema.ObjectId, ref: "User" }],
comments: [{ type: Schema.ObjectId, ref: "Comment" }],
attachments: {
images: [], videos: []
},
loc : { type: {type:String}, coordinates: [Number]},
isActive: { type: Boolean, 'default': true },
createdAt: { type: Date, 'default': Date.now }
});
// PostSchema.index({loc: '2dsphere'});
module.exports = mongoose.model('Post', PostSchema);
var CommentSchema = new Schema({
comment_user: { type: Schema.ObjectId, ref: "User" },
comment_text: String,
isActive: { type: Boolean, 'default': true },
createdAt: { type: Date, 'default': Date.now }
});
module.exports = mongoose.model('Comment', CommentSchema);
这是控制器
Post = require('./post.model');
var postMapper = require('./post.mapper');
var pm = new postMapper.PostMapper();
exports.create = function (req: Request, res: Response) {
var user = req.user;
var postModel = pm.toSchema(req.body,user);
postModel.save((err, post) => {
if (err) { return res.send(err); }
res.status(201).json({ error: false, post: post });
});
};
这是Mapper
var Post = require('./post.model');
export class PostMapper {
//new post creation
toSchema(data, user) {
var attachments = data.attachments ? { images: data.attachments.images, videos: data.attachments.videos } : {}
return new Post({
post_text: data.post_text,
// loc: { type: "Point", coordinates: [data.longitude, data.latitude] },
attachments: attachments,
post_author: user._id
})
}
}
我没有收到任何错误但是帖子表中仍然没有文档。 我检查了数据中是否存在正确的值,我在上面的代码中传递给Mapper的方法。
请帮忙
答案 0 :(得分:0)
自己找到答案
问题是因为我在同一个文件中使用了两个导出。
我刚刚把它分开并且工作正常。