猫鼬地图集连接挂

时间:2019-03-25 00:18:22

标签: node.js mongodb mongoose mongoose-schema

我正在尝试为MEAN堆栈抽象一个教程,以使用MongoAtlas而不是本地实例。 我与Atlas DB的猫鼬连接正在运行(据我所能连接的数据库)。但是我无法执行save()。我相信这与我创建连接对象的方式有关,但是我似乎无法解决这个问题。

我以为通过添加bufferCommands = false可以告诉我它不起作用的原因是我错误地打开了连接对象,但是我没有看到任何错误-只是挂了。

product.js

var mongoose = require('mongoose');
var db = mongoose.connection;
var Schema = mongoose.Schema;

var schema = new Schema ({
    imagePath: {type: String, required: true},
    title: {type: String, required: true},
    description: {type: String, required: true},
    price: {type: Number, required:true}
});
var connection = mongoose.createConnection('mongodb+srv://<user>:<password>@cluster0-dpwmr.gcp.mongodb.net/Shopping?retryWrites=true', {useNewUrlParser:true})

module.exports = connection.model('Product', schema);

product-seeder.js

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

var mongoose = require('mongoose');
//mongoose.set('bufferCommands', false);
var products =[ 
    new Product({
        imagePath:'https://picsum.photos/250/?random',
        title: 'Item 1',
        description: 'This is a thing',
        price: 10
        }),
    new Product({
        imagePath:'https://picsum.photos/251/?random',
        title: 'Item 2',
        description: 'This is another thing',
        price: 14
        }),
    new Product({
        imagePath:'https://picsum.photos/253/?random',
        title: 'Item 3',
        description: 'This is a slightly longer description',
        price: 30
        })
];


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

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

我希望这样做可以创建一个新的“产品”文档并在Shopping集合中写入该文档,但这并没有发生,什么也没做。

1 个答案:

答案 0 :(得分:0)

我将以下各项结合使用:

  • 使用mongoose.connect()
  • 将数据库名称添加到选项对象
  • 确保数据库写入是通过正确的承诺完成的,而不是怪异的循环。
var mongoose = require('mongoose');
var db = mongoose.connection;
var Schema = mongoose.Schema;

var schema = new Schema ({
    imagePath: {type: String, required: true},
    title: {type: String, required: true},
    description: {type: String, required: true},
    price: {type: Number, required:true}
});
mongoose.connect('mongodb+srv://<UserName>:<Password>@cluster0-dpwmr.gcp.mongodb.net/', {dbName:'Shopping', useNewUrlParser:true})
.then( () => {
    console.log('Connection to the Atlas Cluster is successful!')
  })
  .catch( (err) => console.error(err));

const conn = mongoose.connection;
module.exports = mongoose.model('Product', schema);
var Product = require('../models/product');

var mongoose = require('mongoose');

var products =[ 
    new Product({
        imagePath:'https://picsum.photos/250/?random',
        title: 'Item 1',
        description: 'This is a thing',
        price: 10
        }),
    new Product({
        imagePath:'https://picsum.photos/251/?random',
        title: 'Item 2',
        description: 'This is another thing',
        price: 14
        }),
    new Product({
        imagePath:'https://picsum.photos/253/?random',
        title: 'Item 3',
        description: 'This is a slightly longer description',
        price: 30
        })
];
const conn = mongoose.connection;

processArray(products);

async function processArray (array) {
   for (const item of products){
       console.log (item)
       await item.save();
   }
   conn.close();
}