因此,对于Express框架和Mongoose数据库,我是一个相当陌生的人,我主要使用关系数据库。我想使用以下脚本创建数据库,使mongod.exe在localhost:27017上运行并监听,并且可以通过mongo.exe和http连接到它。但是,当我尝试使用节点nameOfTheScript运行脚本时,我不断


  (节点:1000)DeprecationWarning:不建议使用当前的URL字符串解析器,并且在以后的版本中将其删除
  sion。要使用新的解析器,请将选项{useNewUrlParser:true}传递给MongoClient.connect。
  最终错误:ValidationError:体裁:路径genre是必需的。


我的脚本:



var async = require('async');
var Book = require('./models/book');
var Author = require('./models/author');
var Genre = require('./models/genre');
var BookInstance = require('./models/bookinstance');


var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/');

mongoose.Promise = global.Promise;
var db = mongoose.connection;
mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection error:'));


var authors = []
var genres = []
var books = []
var bookinstances = []

function authorCreate(first_name, family_name, d_birth, d_death, cb) {
    authordetail = {first_name:first_name , family_name: family_name }
    if (d_birth != false) authordetail.date_of_birth = d_birth
    if (d_death != false) authordetail.date_of_death = d_death

    var author = new Author(authordetail);

    author.save(function (err) {
        if (err) {
            cb(err, null)
            return
        }
        console.log('New Author: ' + author);
        authors.push(author)
        cb(null, author)
    }  );
}

function genreCreate(name, cb) {
    var genre = new Genre({ name: name });

    genre.save(function (err) {
        if (err) {
            cb(err, null);
            return;
        }
        console.log('New Genre: ' + genre);
        genres.push(genre)
        cb(null, genre);
    }   );
}

function bookCreate(title, summary, isbn, author, genre, cb) {
    bookdetail = {
        title: title,
        summary: summary,
        author: author,
        isbn: isbn
    }
    if (genre != false) bookdetail.genre = genre

    var book = new Book(bookdetail);
    book.save(function (err) {
        if (err) {
            cb(err, null)
            return
        }
        console.log('New Book: ' + book);
        books.push(book)
        cb(null, book)
    }  );
}


function bookInstanceCreate(book, imprint, due_back, status, cb) {
    bookinstancedetail = {
        book: book,
        imprint: imprint
    }
    if (due_back != false) bookinstancedetail.due_back = due_back
    if (status != false) bookinstancedetail.status = status

    var bookinstance = new BookInstance(bookinstancedetail);
    bookinstance.save(function (err) {
        if (err) {
            console.log('ERROR CREATING BookInstance: ' + bookinstance);
            cb(err, null)
            return
        }
        console.log('New BookInstance: ' + bookinstance);
        bookinstances.push(bookinstance)
        cb(null, book)
    }  );
}


function createGenreAuthors(cb) {
    async.parallel([
            function(callback) {
                authorCreate('Patrick', 'Rothfuss', '1973-06-06', false, callback);
            },
            function(callback) {
                authorCreate('Ben', 'Bova', '1932-11-8', false, callback);
            },
            function(callback) {
                authorCreate('Isaac', 'Asimov', '1920-01-02', '1992-04-06', callback);
            },
            function(callback) {
                authorCreate('Bob', 'Billings', false, false, callback);
            },
            function(callback) {
                authorCreate('Jim', 'Jones', '1971-12-16', false, callback);
            },
            function(callback) {
                genreCreate("Fantasy", callback);
            },
            function(callback) {
                genreCreate("Science Fiction", callback);
            },
            function(callback) {
                genreCreate("French Poetry", callback);
            },
        ],
        // optional callback
        cb);
}


function createBooks(cb) {
    async.parallel([
            function(callback) {
                bookCreate('The Name of the Wind (The Kingkiller Chronicle, #1)', 'I have stolen princesses back from sleeping barrow kings. I burned down the town of Trebon. I have spent the night with Felurian and left with both my sanity and my life. I was expelled from the University at a younger age than most people are allowed in. I tread paths by moonlight that others fear to speak of during day. I have talked to Gods, loved women, and written songs that make the minstrels weep.', '9781473211896', authors[0], [genres[0],], callback);
            },
            function(callback) {
                bookCreate("The Wise Man's Fear (The Kingkiller Chronicle, #2)", 'Picking up the tale of Kvothe Kingkiller once again, we follow him into exile, into political intrigue, courtship, adventure, love and magic... and further along the path that has turned Kvothe, the mightiest magician of his age, a legend in his own time, into Kote, the unassuming pub landlord.', '9788401352836', authors[0], [genres[0],], callback);
            },
            function(callback) {
                bookCreate("The Slow Regard of Silent Things (Kingkiller Chronicle)", 'Deep below the University, there is a dark place. Few people know of it: a broken web of ancient passageways and abandoned rooms. A young woman lives there, tucked among the sprawling tunnels of the Underthing, snug in the heart of this forgotten place.', '9780756411336', authors[0], [genres[0],], callback);
            },
            function(callback) {
                bookCreate("Apes and Angels", "Humankind headed out to the stars not for conquest, nor exploration, nor even for curiosity. Humans went to the stars in a desperate crusade to save intelligent life wherever they found it. A wave of death is spreading through the Milky Way galaxy, an expanding sphere of lethal gamma ...", '9780765379528', authors[1], [genres[1],], callback);
            },
            function(callback) {
                bookCreate("Death Wave","In Ben Bova's previous novel New Earth, Jordan Kell led the first human mission beyond the solar system. They discovered the ruins of an ancient alien civilization. But one alien AI survived, and it revealed to Jordan Kell that an explosion in the black hole at the heart of the Milky Way galaxy has created a wave of deadly radiation, expanding out from the core toward Earth. Unless the human race acts to save itself, all life on Earth will be wiped out...", '9780765379504', authors[1], [genres[1],], callback);
            },
            function(callback) {
                bookCreate('Test Book 1', 'Summary of test book 1', 'ISBN111111', authors[4], [genres[0],genres[1]], callback);
            },
            function(callback) {
                bookCreate('Test Book 2', 'Summary of test book 2', 'ISBN222222', authors[4], false, callback)
            }
        ],
        // optional callback
        cb);
}


function createBookInstances(cb) {
    async.parallel([
            function(callback) {
                bookInstanceCreate(books[0], 'London Gollancz, 2014.', false, 'Available', callback)
            },
            function(callback) {
                bookInstanceCreate(books[1], ' Gollancz, 2011.', false, 'Loaned', callback)
            },
            function(callback) {
                bookInstanceCreate(books[2], ' Gollancz, 2015.', false, false, callback)
            },
            function(callback) {
                bookInstanceCreate(books[3], 'New York Tom Doherty Associates, 2016.', false, 'Available', callback)
            },
            function(callback) {
                bookInstanceCreate(books[3], 'New York Tom Doherty Associates, 2016.', false, 'Available', callback)
            },
            function(callback) {
                bookInstanceCreate(books[3], 'New York Tom Doherty Associates, 2016.', false, 'Available', callback)
            },
            function(callback) {
                bookInstanceCreate(books[4], 'New York, NY Tom Doherty Associates, LLC, 2015.', false, 'Available', callback)
            },
            function(callback) {
                bookInstanceCreate(books[4], 'New York, NY Tom Doherty Associates, LLC, 2015.', false, 'Maintenance', callback)
            },
            function(callback) {
                bookInstanceCreate(books[4], 'New York, NY Tom Doherty Associates, LLC, 2015.', false, 'Loaned', callback)
            },
            function(callback) {
                bookInstanceCreate(books[0], 'Imprint XXX2', false, false, callback)
            },
            function(callback) {
                bookInstanceCreate(books[1], 'Imprint XXX3', false, false, callback)
            }
        ],
        // Optional callback
        cb);
}



async.series([
        createGenreAuthors,
        createBooks,
        createBookInstances
    ],
// Optional callback
    function(err, results) {
        if (err) {
            console.log('FINAL ERR: '+err);
        }
        else {
            console.log('BOOKInstances: '+bookinstances);

        }
        // All done, disconnect from database
        mongoose.connection.close();
    });





经过数小时的尝试仍然找不到解决方案后,我将如何解决该错误。

感谢帮助

最佳答案

mongoose.connect('mongodb://localhost:27017/myapp', **{useNewUrlParser: true}**);


从官方文档:


  useNewUrlParser-基础的MongoDB驱动程序已弃用其
  当前的连接字符串解析器。因为这是一个重大变化,所以他们
  添加了useNewUrlParser标志,以允许用户使用旧的
  如果他们在新的解析器中发现错误,则解析器。你应该设置
  useNewUrlParser:true,除非它阻止您连接。注意
  如果您指定useNewUrlParser:true,则必须在
  您的连接字符串,例如mongodb:// localhost:27017 / dbname。新的
  网址解析器不支持没有
  端口,例如mongodb:// localhost / dbname。

07-24 00:35