使用节点错误播种mongodb

时间:2018-01-21 01:37:57

标签: node.js mongodb express mongoose npm

nodejs,express,mongoose项目 试图为数据库播种 mongo激活,但是当我运行以下内容时出现错误:

$ node product-seeder.js

(node:2810)UnhandledPromiseRejectionWarning:错误:架构无效,预计mongodbmongodb+srv

at module.exports (/Users/MXD/Sites/adx/hosted/ShoppingCartwithNodeJSExpressAuthentic/shopping-cart/node_modules/mongodb/lib/url_parser.js:15:21)
at Promise (/Users/MXD/Sites/adx/hosted/ShoppingCartwithNodeJSExpressAuthentic/shopping-cart/node_modules/mongoose/lib/connection.js:330:5)
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (/Users/MXD/Sites/adx/hosted/ShoppingCartwithNodeJSExpressAuthentic/shopping-cart/node_modules/mongoose/lib/connection.js:329:17)
at Mongoose.connect (/Users/MXD/Sites/adx/hosted/ShoppingCartwithNodeJSExpressAuthentic/shopping-cart/node_modules/mongoose/lib/index.js:206:15)
at Object.<anonymous> (/Users/MXD/Sites/adx/hosted/ShoppingCartwithNodeJSExpressAuthentic/shopping-cart/seed/product-seeder.js:5:10)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
at startup (bootstrap_node.js:193:16)
at bootstrap_node.js:617:3

产品seeder.js

var Product = require('../models/product');

var mongoose = require('mongoose');

mongoose.connect('localhost:27017/shopping');

var products = [
    new Product({
      imagePath: 'images/black_tee_flagship_p_symbol_right_340x380.png',
      title: 'Black Tee White P',
      description: 'P on chest [white], symbols on sleeve',
      price: 28
    }),
    new Product({
      imagePath: 'images/white_tee_purple_eye_pocket_340x380.png',
      title: 'White Tee Purple Eye',
      description: 'Embroidered Purple Eye',
      price: 28
    }),
    new Product({
      imagePath: 'images/black_tee_gold_dual_p_center_340x380.png',
      title: 'Black Tee Gold 2Ps',
      description: 'Black Tee 2Ps on Chest [gold]',
      price: 28
    }),
    new Product({
      imagePath: 'images/white_tee_p_symbol_popular_text_340x380.png',
      title: 'White Tee Purple P',
      description: 'P Logo + POPULAR underneath',
      price: 28
    }),
    new Product({
      imagePath: 'images/black_tee_blue_dual_p_symbol_right_dual_p_left_340x380.png',
      title: 'Black Tee Blue 2Ps',
      description: '2Ps on chest, symbols + 2ps on sleeve',
      price: 28
    }),
    new Product({
      imagePath: 'images/white_tee_popular_blackboxed_center_340x380.png',
      title: 'White Tee POPULAR',
      description: 'POPULAR on chest [white on black]',
      price: 28
    })
];

var done = 0;

for (var i = 0; i < products.length; i++) {
    products[i].save(function(err, result) {
        done++;
        if (done === products.length) {
            exit();
        }
    });
}

function exit() {
    mongoose.disconnect();
}

1 个答案:

答案 0 :(得分:1)

您的连接字符串需要更新:它缺少URL架构。

您的连接字符串必须是:

mongoose.connect('mongodb://localhost/shopping');

由于27017是MongoDB的默认端口,因此您可以将其保留。

More infos