【问题标题】:Unable to write to mongodb无法写入mongodb
【发布时间】:2013-06-06 08:55:47
【问题描述】:

我正在尝试使用 node.js 填充我的 mongo db。但我收到一个 TypeError 的说法

'对象#没有应用方法

.

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
console.log('Running mongoose version %s:', mongoose.version);



var userSchema = Schema({
    userName: String,
    fullName: String,
    email: String
});
var articleSchema = Schema({
    name: String,
    content_date: Date,
    content: String,
    author: String
});

var User = mongoose.model('User', userSchema);
var Article = mongoose.model('Article', articleSchema);

var createData = function () {
    User.create({userName: 'mcslater', fullName: 'Ray Slater', email: 'r.slater@nomail'}, function (err, user) {
        Article.create({
                "name": "NEW YORK MAGAZINE",
                "content": "Heaven, with hangers. <a href='http://nymag.com/fashion/08/fall/49258/index4.html' target='_blank'>New York Magazine</a> declares the MKG Wardrobe 'gorgeous' in the Fall Fashion '08 issue.",
                "content_date": "Mon Aug 18 15:51:49 +0000 2008",
                "author": user.userName
            },
            {
                "name": "DWELL",
                "content": "Michael Cannell of <a href='http://www.dwell.com' target='_blank'>Dwell</a>  calls our work for the East 11th Townhouse 'artful' in a brief piece for Dwell.com.",
                "content_date": "Tue June 17 12:00:00 +0000 2008",
                "author": user.userName
            },
            {
                "name": "DWELL",
                "content": "The Josh & Donna Loft catches the eye of Michael Cannell at <a href='http://www.dwell.com' target='_blank'>Dwell.com</a>&nbps;.",
                "content_date": "Tue Nov 27 12:00:00 +0000 2007",
                "author": user.userName
            },
            {
                "name": "ARCHITECTURAL RECORD",
                "content": "Heavy metal, light touch. <a href='http://archrecord.construction.com/archrecord2/work/0710/associatedFabrication.asp' target='_blank'>Archrecord2</a> profiles 4-pli and our sister company, Associated Fabrication, in the October issue, out now.",
                "content_date": "Mon Oct 8 12:00:00 +0000 2007",
                "author": user.userName
            },
            {
                "name": "OUR COTTON ANNIVERSARY",
                "content": "On this day in history, way back in 2005, 4-pli was formed. Happy anniversary to us!",
                "content_date": "Tue Oct 01 12:00:00 +0000 2007",
                "author": user.userName
            },
            {
                "name": "COOL HUNTING",
                "content": "Our Nesting Desk makes the cut at Coolhunting.",
                "content_date": "Wed Apr 04 12:00:00 +0000 2007",
                "author": user.userName
            })
    })
};


mongoose.connect('localhost','news',function(err){
    if(err) throw err;
    createData();
});

【问题讨论】:

    标签: javascript node.js nosql mongoose


    【解决方案1】:

    我想你忘记了“新”关键字:

    var userSchema = new Schema({ // <-- new 
        userName: String,
        fullName: String,
        email: String
    });
    
    var articleSchema = new Schema({ // <-- new
        name: String,
        content_date: Date,
        content: String,
        author: String
    });
    

    【讨论】:

    • 你是对的,但这仍然不能解决问题。感谢您指出这一点。
    • hmm...好吧,如果不是太麻烦的话,你可以添加一个回调函数作为Article.create()的最后一个参数吗?就像你在 User.create() 中一样。也许错误来自那里......
    • 出于某种原因,我不得不从 mongoose.connect 的回调中删除 CreateData()。
    猜你喜欢
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 2016-04-04
    相关资源
    最近更新 更多