Giter Site home page Giter Site logo

Comments (15)

tfogo avatar tfogo commented on July 29, 2024 1

Let me know if anything is unclear or if there are any more errors.

from mean-contactlist-angular2.

tfogo avatar tfogo commented on July 29, 2024

Hey, happy to help! Please can you share the error that is printed to the console? Sometimes the log file misses out information.

from mean-contactlist-angular2.

nameisvick avatar nameisvick commented on July 29, 2024

i've attched the image of the command prompt which displays the error msg and you can also see the package.json
image

from mean-contactlist-angular2.

tfogo avatar tfogo commented on July 29, 2024

Hey,

Can you copy/paste or send a screenshot of the whole output from running npm start on the command line? I think there might be more above the lines you've shown? That might help diagnose what the problem is.

from mean-contactlist-angular2.

nameisvick avatar nameisvick commented on July 29, 2024

hello , i guess this must be what you are asking for

image [ throw new TypeError('Parameter "url" must be a string, not ' + typeof url);
^

typeError: Parameter "url" must be a string, not undefined
at Url.parse (url.js:81:11)
at Object.urlParse [as parse] (url.js:75:5)
at module.exports (D:\mean-contactlist-angular2-master\node_modules\mongodb
lib\url_parser.js:16:23)
at connect (D:\mean-contactlist-angular2-master\node_modules\mongodb\lib\mon
go_client.js:486:16)
at Function.MongoClient.connect (D:\mean-contactlist-angular2-master\node_mo
dules\mongodb\lib\mongo_client.js:250:3)
at Object. (D:\mean-contactlist-angular2-master\server.js:19:21)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)

]
(hopefully you can help me with this)
and btw sorry for the late reply

from mean-contactlist-angular2.

tfogo avatar tfogo commented on July 29, 2024

Hey,

Yep! That's exactly it. The issue is you don't have the MONGODB_URI environment variable set. You need to set this variable to tell the program where your MongoDB database is.

In the tutorial, we provision an mLab database on heroku. That automatically sets the MONGODB_URI environment variable to the URL of your MongoDB database.

If you're running this on your own computer, you're going to have to do this step manually. Here are instructions to set up a free Sandbox database on mlab. It's quick to set up. When you create a database, you'll see a connection string on your database's home page which looks like this:

mongodb://<dbuser>:<dbpassword>@ds012345.mlab.com:56789/mydb

Create a username and password for your database under the users tab. Then you can run this from your command line:

set MONGODB_URI=mongodb://<dbuser>:<dbpassword>@ds012345.mlab.com:56789/mydb

Where <dbuser> and <dbpassword> are the username and password you just made for the database on the users tab. This sets the MONGODB_URI environment variable.

Now when you run npm start it should work.

A couple of notes:

  • If you have MongoDB installed locally, you can use the connection string mongodb://localhost/mydb to use your local database.
  • Every time you restart cmd, you'll need to set the MONGODB_URI environment variable again. To set it forever, you can use the setx command:
    setx MONGODB_URI mongodb://<dbuser>:<dbpassword>@ds012345.mlab.com:56789/mydb
    

Let me know if that works. If you have any issues, I'm here to help.

from mean-contactlist-angular2.

nameisvick avatar nameisvick commented on July 29, 2024

Thank you so much ... actually i've installed MongoDB locally i cannot find where to insert that connection string .. can you explain that thing ?

from mean-contactlist-angular2.

tfogo avatar tfogo commented on July 29, 2024

You donโ€™t have to change any file. You run this from the command line:

set MONGODB_URI=mongodb://localhost/mydb

That points to the โ€œmydbโ€ database on your local machine. The server uses this variable to know where to connect to your database.

Then run npm start and it should work.

from mean-contactlist-angular2.

nameisvick avatar nameisvick commented on July 29, 2024

hai again now i have no issue in running the program .. but am having issues in my console ...

image

hopefully you can help me fix this .. Thank's in advance

from mean-contactlist-angular2.

chrisckchang avatar chrisckchang commented on July 29, 2024

@nameisvick can you please upload your project to github and provide us with a link? we'll see if we can reproduce and debug on our end.

from mean-contactlist-angular2.

nameisvick avatar nameisvick commented on July 29, 2024

actually i havent done (modified) anything in the project . i have just downloaded from this link (https://github.com/chrisckchang/mean-contactlist-angular2) and trying to run this project from this tutorial (https://devcenter.heroku.com/articles/mean-apps-restful-api#provision-a-mongodb-database)....

from mean-contactlist-angular2.

tfogo avatar tfogo commented on July 29, 2024

Hey,

It looks like you're running ng serve to start the application. This will serve the AngularJS part of the application but not the NodeJS part.

Make sure you're running npm start to start up the server - this will serve both the AngularJS and the NodeJS parts of the application.

When you run npm start, this is the output you should see on the command line:

Database connection ready
App now running on port 8080

If you get something different, go into your package.json file and make sure it correctly says "start": "node server.js" (see here).

Let us know if this fixes it.

from mean-contactlist-angular2.

nameisvick avatar nameisvick commented on July 29, 2024

image
hai again ..in the above image shows my package.json in which says "start": "node server.js" and when i give npm start it says "Connected sucessfully to server" it does not open in any port ...

here is my server.js

var express = require("express");
var bodyParser = require("body-parser");
var mongodb = require("mongodb");
var ObjectID = mongodb.ObjectID;

var CONTACTS_COLLECTION = "contacts";

var app = express();
app.use(bodyParser.json());

// Create a database variable outside of the database connection callback to reuse the connection pool in your app.
/*var db;

// Connect to the database before starting the application server.
mongodb.MongoClient.connect(process.env.MONGODB_URI, function (err, database) {
  if (err) {
    console.log(err);
    process.exit(1);
  }

  // Save database object from the callback for reuse.
  db = database;
  console.log("Database connection ready");

  // Initialize the app.
  var server = app.listen(process.env.PORT || 8080, function () {
    var port = server.address().port;
    console.log("App now running on port", port);
  });
});
*/


var MongoClient = require('mongodb').MongoClient
  , assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  db.close();
});
// CONTACTS API ROUTES BELOW

// Generic error handler used by all endpoints.
function handleError(res, reason, message, code) {
  console.log("ERROR: " + reason);
  res.status(code || 500).json({"error": message});
}

/*  "/api/contacts"
 *    GET: finds all contacts
 *    POST: creates a new contact
 */

app.get("/api/contacts", function(req, res) {
  db.collection(CONTACTS_COLLECTION).find({}).toArray(function(err, docs) {
    if (err) {
      handleError(res, err.message, "Failed to get contacts.");
    } else {
      res.status(200).json(docs);
    }
  });
});

app.post("/api/contacts", function(req, res) {
  var newContact = req.body;
  newContact.createDate = new Date();

  if (!req.body.name) {
    handleError(res, "Invalid user input", "Must provide a name.", 400);
  }

  db.collection(CONTACTS_COLLECTION).insertOne(newContact, function(err, doc) {
    if (err) {
      handleError(res, err.message, "Failed to create new contact.");
    } else {
      res.status(201).json(doc.ops[0]);
    }
  });
});

/*  "/api/contacts/:id"
 *    GET: find contact by id
 *    PUT: update contact by id
 *    DELETE: deletes contact by id
 */

app.get("/api/contacts/:id", function(req, res) {
  db.collection(CONTACTS_COLLECTION).findOne({ _id: new ObjectID(req.params.id) }, function(err, doc) {
    if (err) {
      handleError(res, err.message, "Failed to get contact");
    } else {
      res.status(200).json(doc);
    }
  });
});

app.put("/api/contacts/:id", function(req, res) {
  var updateDoc = req.body;
  delete updateDoc._id;

  db.collection(CONTACTS_COLLECTION).updateOne({_id: new ObjectID(req.params.id)}, updateDoc, function(err, doc) {
    if (err) {
      handleError(res, err.message, "Failed to update contact");
    } else {
      updateDoc._id = req.params.id;
      res.status(200).json(updateDoc);
    }
  });
});

app.delete("/api/contacts/:id", function(req, res) {
  db.collection(CONTACTS_COLLECTION).deleteOne({_id: new ObjectID(req.params.id)}, function(err, result) {
    if (err) {
      handleError(res, err.message, "Failed to delete contact");
    } else {
      res.status(200).json(req.params.id);
    }
  });
});

from mean-contactlist-angular2.

tfogo avatar tfogo commented on July 29, 2024

It looks like you've commented out the code that starts the server with /* and */ so it won't run:

// Create a database variable outside of the database connection callback to reuse the connection pool in your app.
/*var db;

// Connect to the database before starting the application server.
mongodb.MongoClient.connect(process.env.MONGODB_URI, function (err, database) {
  if (err) {
    console.log(err);
    process.exit(1);
  }

  // Save database object from the callback for reuse.
  db = database;
  console.log("Database connection ready");

  // Initialize the app.
  var server = app.listen(process.env.PORT || 8080, function () {
    var port = server.address().port;
    console.log("App now running on port", port);
  });
});
*/

This is the part of that code which starts the server:

var server = app.listen(process.env.PORT || 8080, function () {
   var port = server.address().port;
   console.log("App now running on port", port);
});

It looks like you've also added this code in its place:

var MongoClient = require('mongodb').MongoClient
, assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected successfully to server");

db.close();
});

You will not be able to use the database with this code. This code connects to the database then immediately closes the connection. This is because you call db.close() as soon as you connect to the database.

You should go back to the original code. The original code will work when you set your environment variable using set MONGODB_URI=mongodb://localhost:27017/myproject. You can copy the original code from here: https://github.com/chrisckchang/mean-contactlist-angular2/blob/master/server.js.

Let us know if that works or if you have any further problems.

from mean-contactlist-angular2.

tfogo avatar tfogo commented on July 29, 2024

It's been a month so I'm going to close this. If you have any further questions, do let us know ๐Ÿ™‚

from mean-contactlist-angular2.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.