Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Mongoose无法创建新文档_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

Node.js Mongoose无法创建新文档

Node.js Mongoose无法创建新文档,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我将文章模型定义为: var ArticleSchema = new Schema({ type: String ,title: String ,content: String ,comments: [{ type: Schema.ObjectId ,ref: 'Comment' }] ,replies: [{ type: Schema.ObjectId ,ref: 'Reply

我将文章模型定义为:

var ArticleSchema = new Schema({

    type: String
    ,title: String
    ,content: String

    ,comments: [{
        type: Schema.ObjectId
        ,ref: 'Comment'
    }]

    ,replies: [{
        type: Schema.ObjectId
        ,ref: 'Reply'
    }]


    ,feedbacks: [{
        type: Schema.ObjectId
        ,ref: 'Feedback'
    }]

    ,meta: {
        tags: [String] //anything
        ,apps: [{
            store: String //app store, google play, amazon app store
            ,storeId: String
        }]
        ,category: String
    }


    //normal, deleted, banned
    , status: String
    ,statusMeta: {
        createdBy: {
            type: Schema.ObjectId
            ,ref: 'User'
        }
        ,createdDate: Date
        , updatedBy: {
            type: Schema.ObjectId
            ,ref: 'User'
        }
        ,updatedDate: Date

        ,deletedBy: {
            type: Schema.ObjectId,
            ref: 'User'
        }
        ,deletedDate: Date

        ,undeletedBy: {
            type: Schema.ObjectId,
            ref: 'User'
        }
        ,undeletedDate: Date

        ,bannedBy: {
            type: Schema.ObjectId,
            ref: 'User'
        }
        ,bannedDate: Date
        ,unbannedBy: {
            type: Schema.ObjectId,
            ref: 'User'
        }

        ,unbannedDate: Date
    }
})
我有以下代码来创建一篇新文章并保存它

var newArticle = new Article()
newArticle.status = helper.constant.articleTypes.other
newArticle.type = req.body.type
newArticle.category = req.body.category
newArticle.title = req.body.title
newArticle.content = req.body.content
newArticle.meta = req.body.meta
newArticle.statusMeta.createdBy = req.user
newArticle.statusMeta.createdDate = new Date
newArticle.save(function(err) {
    if (err)
        return next(err)
}
我的预保存挂钩(助手功能)

helper.validation定义如下。它基本上是一组函数,接收输入并返回错误消息(如果有的话)。如果没有错误,只需返回
'

当我试图创建一篇新文章时,我得到一个错误,即需要
创建日期

我尝试了
newArticle.markModified('statusMeta')
和newArticle.markModified(
statusMeta.createdDate
),两者都不起作用。我认为没有必要将其标记为已修改,因为它是
嵌套对象
类型,而不是
混合
类型(来自mongoose文档)

我还尝试设置
newArticle.statusMeta={}
,但也不起作用

设置断点时,newArticle.statusMeta.createdDate未定义


我不想为createdDate使用默认值的原因是,在运行pre('save')hook之前设置默认值,这使得我的验证代码总是失败,这是我自己的错误。在
helper.js中
我使用了
helper.funcName()
而不是
exports.funcName()
。即使使用webstorm IDE,也很难用javascript进行调试

exports.batchValidationWrapper = function(schema, module, fieldPaths) {
    for (var i = 0; i < fieldPaths.length; i++) {
        var fieldPath = fieldPaths[i]
        ;(function(fieldPath) {
            schema.pre('save', true, function(next, done) {
                var self = this

                var validationFunction = exports.validation[module][fieldPath]
                var msg = validationFunction(self[fieldPath])
                if (msg) {
                    self.invalidate(fieldPath, msg)
                    done(msg)
                }
                else {
                    done()
                }

            })
        })(fieldPath)
    }
}
helper.batchValidationWrapper(ArticleSchema, 'article', [
    'type'
    ,'title'
    ,'content'
    ,'comments'
    ,'replies'
    ,'feedbacks'
    ,'meta.tags'
    ,'meta.apps'
    ,'meta.category'
    ,'status'
    ,'statusMeta.createdBy'
    ,'statusMeta.createdDate'
    ,'statusMeta.deletedBy'
    ,'statusMeta.deletedDate'
    ,'statusMeta.undeletedBy'
    ,'statusMeta.undeletedDate'
    ,'statusMeta.bannedBy'
    ,'statusMeta.bannedDate'
    ,'statusMeta.unbannedBy'
    ,'statusMeta.unbannedDate'


])
exports.article = {

    type: function(input) {
        if (!input)
            return 'type is requried'
        return passIfAmongTypes('Article', input, constant.articleTypes)
    }

    ,'statusMeta.createdDate': function(input) {
        if (!input)
            return 'created date is required'
        return ''
    }
}