简体   繁体   中英

Unable to write to mongodb

I'm trying to populate my mongo db using node.js. But I'm getting a TypeError saying

' Object # has no method apply

.

在此处输入图片说明

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();
});

i think you forgot the 'new' keyword:

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
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM