TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received undefined

I got this error while seed the data from nodejs to mongo DB

Hello @Vimal_Kumar_G, Welcome to the MongoDB community forum,

The error says, provided url argument has a non-string value so make sure by printing/consol the url property has the correct value or not.

More debugging details would be great if still not resolved the problem.

Hi @turivishal thank you for your response, I’m beginner for node js and mongodb can u please explain me how to do that…

This is my seed.js code

const { faker } = require('@faker-js/faker');
const { MongoClient } = require('mongodb')

const _ = require("lodash");

const { connection } = require('mongoose');

async function main() {

    const uri = 'mongodb://localhost:27017';

    const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });

    try {

        await  client.connect();
        const productsCollection = client.db("e-canteen").collection("products");
        const categoriesCollection = client.db("e-canteen").collection("categories");
        let categories = ['breakfast', 'lunch', 'dinner', 'drinks'].map((category) => { return { name: category } });
        await categoriesCollection.insertMany(categories);

        let imageUrls = [
            'https://asset.cloudinary.com/dbqvp70fu/9bb1282387d2e870bd764a7cbd36908f',
            'https://asset.cloudinary.com/dbqvp70fu/38d8ef5bc366f5a0906f982329bd6b18',
            'https://asset.cloudinary.com/dbqvp70fu/c5156be531cbbd2e1c1adf4258ab77d0'
        ]

        let products = [];

        for (let i = 0; i < 10; i+=1) {
            let newProduct = {
                name: faker.commerce.productName(),
                desciption: faker.commerce.productDescription(),
                price: faker.commerce.price(),
                category: _.sample(categories),
                imageUrl: _.sample(imageUrls)
            };
            products.push(newProduct);
        }
        await productsCollection.insertMany(products);

    } catch (e) {

        console.error(e);

    } finally {

        await connection.close();

    }
}

main();

Hello @Vimal_Kumar_G,

I tested your provided code, and it is working for me and inserts the data in the database, Just make sure you are executing the same send.js file, or check the package.json file all npms are updated to the latest version.

If you are new to node js and MongoDB then you have to check the free online courses at MongoDB university,

1 Like

Thanks @turivishal can you please send me the other js files used by you to connect with mongo db and config

1 Like

Hello @Vimal_Kumar_G,

There is no need for other js files, I just used your js file only.

1 Like

Thank you Mr. @turivishal