OverwriteModelError: Cannot overwrite`category` model once compiled
发布于 12 年前 作者 qq277049 11048 次浏览 最后一次编辑是 8 年前
C:\nodejs\node.exe app.js
Express server listening on port 81
POST /39 200 1225ms - 3b

D:\node.js\node_modules\mongodb\lib\mongodb\connection\server.js:565
        throw err;
              ^
TypeError: Cannot use 'in' operator to search for '_id' in category
    at model.Document.$__buildDoc (D:\node.js\node_modules\mongoose\lib\document.js:159:27)
    at model.Document (D:\node.js\node_modules\mongoose\lib\document.js:58:20)
    at model.Model (D:\node.js\node_modules\mongoose\lib\model.js:38:12)
    at new model (D:\node.js\node_modules\mongoose\lib\model.js:2112:11)
    at model (D:\node.js\node_modules\mongoose\lib\model.js:2111:14)
    at NativeConnection.<anonymous> (D:\node.js\routes\index.js:76:17)
    at NativeConnection.g (events.js:175:14)
    at NativeConnection.EventEmitter.emit (events.js:92:17)
    at open (D:\node.js\node_modules\mongoose\lib\connection.js:441:10)
    at NativeConnection.Connection.onOpen (D:\node.js\node_modules\mongoose\lib\connection.js:449:5)

Process finished with exit code 8

以上是错误提示,下面是代码

exports.c_39 = function(req,res) {
    var n = require('needle')
    var $ = require('jquery').create()
    var iconv = require('iconv').Iconv;
    var lite = require('iconv-lite')
    var mongoose = require('mongoose');
    var async = require('async')
    var mysql = require('mysql')
    var conn = mysql.createConnection(
        {
            host     : 'localhost',
            user     : 'root',
            password : '',
            database : 'pw'
        }
    )
    conn.connect()
    var title =''
    var question = []
    var answer = []
    var author = [] //作者
    var cid = 0 //分类ID
    var fid = 0 //分类id
    var parentid = 0
    var type = ['category','forum','sub','sub2']
    var fup = ''
    var fupname = ''

    var db = mongoose.createConnection('mongodb://localhost/cj');
    //var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error:'));
    var content = req.body.content
    var url = req.body.url
    if(req.body.pass != 'discuzphpwind')
    {
        req.send('access deind')
        return;
    }
    var cate = []

    //分类抓取
    $(content).find('.ask-sub a[target="_self"]').each(function(i,v){
        cate.push($(v).html().replace(/(^\s*)|(\s*$)/g, ""))
    })


    //分类module
    db.once('open',function(){
        var cat = mongoose.Schema({
            name:String,
            fid:Number,
            parentid:Number,
            type:String
        })
        var t = mongoose.model('category',cat)
        async.series([
            function(callSeries){
                async.eachSeries(cate,function(v,callback){
                    t.findOne({name:v},function(err,value){
                        if(err)
                        {
                            console.log('有错误产生')
                            throw err;
                            return;
                        }
                        var temp = type.shift()
                        //分类不存在
                        if(value == null)
                        {
                            if(cate.length == 3)
                            {
                                if(temp == 'category')
                                {
                                    var issub = 0
                                    var hassub = 1
                                } else if(temp == 'forum') {
                                    var issub = 0
                                    var hassub = 1
                                } else if (temp == 'sub') {
                                    var issub = 1
                                    var hassub = 1
                                }
                            } else if(cate.length == 2) {
                                if(temp == 'category')
                                {
                                    var issub = 0
                                    var hassub = 1
                                } else if(temp == 'forum') {
                                    var issub = 0
                                    var hassub = 0
                                }
                            } else {
                                console.log("还未记录的数量")
                                return;
                            }

                            var sql = "insert into pw_bbs_forum (parentid,type,issub,hassub,name,vieworder,across,fup,fupname,isshow,isshowsub,newtime,created_time" +
                                ",created_userid,created_ip) values(?,?,?,?,?,0,0,?,?,1,0,0,0,0,0)"
                            var sql_values = [fid,temp,issub,hassub,v,fup,fupname]


                            conn.query(sql,sql_values,function(error,results){
                                if(error) {
                                    console.log("插入记录出错" + error.message)
                                    conn.end()
                                    return;
                                }
                                fid = results.insertId
                                conn.query("insert into pw_bbs_forum_statistics (fid) values("+fid+")")
                                var extra = [fid,'a:2:{s:9:"allowtype";a:1:{i:0;s:7:"default";}s:9:"typeorder";a:1:{s:7:"default";i:0;}}']
                                conn.query("insert into pw_bbs_forum_extra (fid,settings_basic) values(?,?)",extra)
                                fupname = v+'\t' + fupname
                                if(fup == '')
                                {
                                    fup = fid
                                } else {
                                    fup =fid + ','+fup
                                }
                                new t({name:v,fid:fid,parentid:parentid,type:temp}).save()
                                if(temp =='category')
                                {
                                    cid = fid
                                }
                                parentid = fid
                                callback(null,v)
                            })
                        } else {
                            if(fupname == '')
                            {
                                fupname = value.name
                            } else {
                                fupname = value.name+'\t' + fupname
                            }

                            fid = value.fid
                            if(fup == '')
                            {
                                fup = fid
                            } else {
                                fup =fid + ','+fup
                            }
                            if(value.type == 'category')
                            {
                                cid = value.fid
                            }
                            parentid = value.parentid
                            callback(null,v)
                        }
                    })
                },function(err){
                    if(err)
                    {
                        console.log('有错误产生')
                        throw err;
                        return;
                    }
                    callSeries(null)
                })
            },
            function(callSeries){
                console.log('b')
                //标题
                title = $(content).find('title').text().split('_39').shift()
                $.each($(content).find('.tboxs'),function(i,v){
                    $(v).find('.anniu').nextAll().remove()
                    $(v).find('.tousu').remove()
                    $(v).find('.anniu').remove()
                    $(v).find('.annius').remove()
                    $(v).find('.cainabox').remove()
                    $(v).find('.clearer').remove()
                    $(v).find('ul').remove()
                    var cc = $(v).find('.tbrig').html()//内容
                    var a = $(v).find('.username').text()
                    if(i == 0)
                    {
                        question = {content:cc,author:a}
                    } else {
                        answer.push({content:cc,author:a})
                    }
                })
                callSeries(null)
            },
            function(callSeries){//信息存储
                var author = mongoose.model('author',mongoose.Schema({
                    id:Number,//存储到mysql中的id
                    name:String//作者的ID
                }))
                async.series([
                    function(callAuthor){//储存问题的作者
                        author.findOne({name:question['author']},function(errData,dataValue){
                            if(errData){
                                throw dataValue
                            }
                            if(dataValue == null)
                            {
                                //添加用户信息
                                var sql = "insert into pw_user (username,email,password,status,groupid,memberid,regdate) values " +
                                    "(?,?,'null',0,1,8,?)"
                                var sql_value = [question['author'],'local[@59n](/user/59n).net',parseInt(new Date().getTime()/1000)]
                                console.log(sql_value)
                                conn.query(sql,sql_value,function(errSql,result){
                                    if(errSql) throw errSql;
                                    //储存到mongodb
                                    new author({name:question['author'],id:result.insertId}).save()
                                    question['author'] = {author:question['author'],uid:result.insertId}
                                    sql = "insert into pw_user_data (uid) values(?)"
                                    sql_value = [result.insertId]
                                    conn.query(sql,sql_value)
                                    callAuthor()
                                })
                            } else {
                                question['author'] = {author:question['author'],uid:dataValue.id}
                                callAuthor()
                            }
                        })
                    },
                    function(callAnswerAuthor){//储存作者
                        async.eachSeries(answer,function(v,callAnswerSeriesAuthor){
                            author.findOne({name:v['author']},function(errData,dataValue){
                                if(errData){
                                    throw dataValue
                                }
                                if(dataValue == null)
                                {
                                    //添加用户信息
                                    var sql = "insert into pw_user (username,email,password,status,groupid,memberid,regdate) values " +
                                        "(?,?,'null',0,1,8,?)"
                                    var sql_value = [v['author'],'local[@59n](/user/59n).net',parseInt(new Date().getTime()/1000)]
                                    conn.query(sql,sql_value,function(errSql,result){
                                        if(errSql) throw errSql;
                                        //储存到mongodb
                                        new author({name:v['author'],id:result.insertId}).save()
                                        v['author'] = {author:v['author'],uid:result.insertId}
                                        sql = "insert into pw_user_data (uid) values(?)"
                                        sql_value = [result.insertId]
                                        conn.query(sql,sql_value)
                                        callAnswerSeriesAuthor()
                                    })
                                } else {
                                    v['author'] = {author:v['author'],uid:dataValue.id}
                                    callAnswerSeriesAuthor()
                                }
                            })
                        },function(err){
                            callAnswerAuthor()
                        })
                    },
                    function(callSaveContent){
                        var m39 = mongoose.Schema({
                            tid:Number,//问题的id
                            date:{type:Date,default:Date.now},
                            answer_num:Number,
                            url:String//采集的url
                        })
                        var c_39 = mongoose.model('c_39',m39)

                        c_39.findOne({url:url},function(errData,dataValue){
                            if(dataValue == null)
                            {
                                //储存到mysql里面
                                var sql = "insert into pw_bbs_threads (fid,subject,ischeck,created_time,created_username,created_userid,created_ip) values" +
                                    "(?,?,?,?,?,?,?)"
                                console.log(question)
                                var sql_values = [fid,title,1,parseInt(new Date().getTime()/1000),question['author'].author,question['author'].uid,'127.0.0.1']
                                conn.query(sql,sql_values,function(errData,result){
                                    if(errData){
                                        console.log('出现异常信息')
                                        throw errData
                                    }
                                    var tid = result.insertId
                                    var temp = [tid,fid,parseInt(new Date().getTime()/1000),parseInt(new Date().getTime()/1000)]
                                    //帖子索引表-新帖索引
                                    conn.query("insert into pw_bbs_threads_index(tid,fid,created_time,lastpost_time) values(?,?,?,?)",temp,function(err,dataValue){
                                        if(err)
                                        {
                                            console.log('pw_bbs_threads_index 这里错了')
                                            throw err
                                        }
                                    })
                                    //增加内容
                                    temp = [tid,question['content']]
                                    conn.query("insert into pw_bbs_threads_content (tid,content,usehtml) values(?,?,1)",temp,function(err,dataValue){
                                        if(err)
                                        {
                                            console.log('pw_bbs_threads_content 这里错了')
                                            throw err
                                        }
                                    })
                                    //帖子索引表-分类索引
                                    temp = [tid,cid,fid,parseInt(new Date().getTime()/1000),parseInt(new Date().getTime()/1000)]
                                    conn.query("insert into pw_bbs_threads_cate_index (tid,cid,fid,created_time,lastpost_time) values(?,?,?,?,?)",temp,function(err,dataValue){
                                        if(err)
                                        {
                                            console.log('pw_bbs_threads_cate_index 这里错了')
                                            throw err
                                        }
                                    })
                                    //回复
                                    $.each(answer,function(i,v){
                                        sql = "insert into pw_bbs_posts (usehtml,fid,tid,ischeck,content,created_time,created_username,created_userid,reply_notice,ipfrom) values" +
                                            "(1,?,?,?,?,?,?,?,?,?)"
                                        sql_values = [fid,tid,1, v.content,parseInt(new Date().getTime()/1000), v['author'].author,v['author'].uid,1,'本机地址']
                                        conn.query(sql,sql_values)
                                        //更新主题
                                        sql = "update pw_bbs_threads set replies = replies + 1 ,lastpost_time = ?,lastpost_userid = ?,lastpost_username = ? where tid = ?"
                                        sql_values = [parseInt(new Date().getTime()/1000),v['author'].uid,v['author'].author,tid]
                                        conn.query(sql,sql_values)
                                    })
                                    //储存到mongodb
                                    var saveData = {
                                        tid:tid,
                                        answer_num:answer.length,
                                        url:url
                                    }
                                    new c_39(saveData).save()

                                })
                            } else {
                                console.log('存在')
                            }
                        })
                    }
                ])
            }
        ])

    })
    res.send('yes')
}
回到顶部