-1

When i run nodemon file.js it show this (cmd) :

(node:23816) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(node:23816) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.

And then when i POST in Postman show this (cmd) : 
events.js:287
      throw er; // Unhandled 'error' event
      ^

ReferenceError: response is not defined
    at D:\Desktop\swag-shop-api\server.js:21:7
    at D:\Desktop\swag-shop-api\node_modules\mongoose\lib\model.js:4889:16
    at D:\Desktop\swag-shop-api\node_modules\mongoose\lib\helpers\promiseOrCallback.js:24:16
    at D:\Desktop\swag-shop-api\node_modules\mongoose\lib\model.js:4912:21
    at model.<anonymous> (D:\Desktop\swag-shop-api\node_modules\mongoose\lib\model.js:493:7)
    at D:\Desktop\swag-shop-api\node_modules\kareem\index.js:315:21
    at next (D:\Desktop\swag-shop-api\node_modules\kareem\index.js:209:27)
    at D:\Desktop\swag-shop-api\node_modules\kareem\index.js:182:9
    at D:\Desktop\swag-shop-api\node_modules\kareem\index.js:507:38
    at processTicksAndRejections (internal/process/task_queues.js:79:11)
Emitted 'error' event on Function instance at:
    at D:\Desktop\swag-shop-api\node_modules\mongoose\lib\model.js:4891:13
    at D:\Desktop\swag-shop-api\node_modules\mongoose\lib\helpers\promiseOrCallback.js:24:16
    [... lines matching original stack trace ...]
    at processTicksAndRejections (internal/process/task_queues.js:79:11)
[nodemon] app crashed - waiting for file changes before starting...

but when i do db.products.find({}) it shows everything i POST form postman

Please can someone help me...

server.js file:

Android Dev
  • 35
  • 1
  • 7
  • 1
    Can you show your events.js file as well? Seems that it is causing the error. –  May 17 '20 at 05:31
  • The two warnings above can be removed by slightly modifying your connect method: ```mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true })``` –  May 17 '20 at 05:32
  • 2
    Can you show the code from your `server.js`? It says `response` is not defined in that file. – Danizavtz May 17 '20 at 09:41
  • Welcome to the Stackoverflow community, here is some tips for a better experience when asking. https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question – Danizavtz May 18 '20 at 01:27
  • @VedantBang where to modify this "mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true })" (I'm new to MongoDB) – Android Dev May 18 '20 at 01:31

2 Answers2

0

If you need some directions to list your documents in a collection in mongodb, just use this code as a starting point:

In line 13 you must change the code to:

app.post("/product", function(request, response) {

The name of the parameter must be response.

Danizavtz
  • 3,166
  • 4
  • 24
  • 25
  • thank you man but now the nodemon app crash in cmd when i run 'nodemon server.js' :( btw im new to Mongodb and stackoverflow ... – Android Dev May 18 '20 at 01:29
  • SyntaxError: Unexpected token ')' at wrapSafe (internal/modules/cjs/loader.js:1070:16) at Module._compile (internal/modules/cjs/loader.js:1120:27) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10) at Module.load (internal/modules/cjs/loader.js:1000:32) at Function.Module._load (internal/modules/cjs/loader.js:899:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) at internal/main/run_main_module.js:18:47 [nodemon] app crashed - waiting for file changes before starting... – Android Dev May 18 '20 at 01:33
  • I found the problem but know shows this: (node:18836) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect. (node:18836) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. Swag Shop API running on port 3000... – Android Dev May 18 '20 at 01:38
  • Yes, that's already commented in original post. There is a comment with a solution. – Danizavtz May 18 '20 at 01:44
  • where do i have to do that ? CMD // mongo and than > mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }) ? – Android Dev May 18 '20 at 01:45
  • In mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }), line 5. Where you do your database connection. – Danizavtz May 18 '20 at 01:47
  • Line 5: var db = mongoose.connect("mongodb://localhost/swag-shop"); after this or in this. – Android Dev May 18 '20 at 01:53
  • nvm i got it, thank you so much is there any way i can review you? – Android Dev May 18 '20 at 01:56
  • We are a community of developers, we are here to help. Thanks. – Danizavtz May 18 '20 at 02:30
0

Solution:

Line 5: var db = mongoose.connect("mongodb://localhost/swag-shop", { useNewUrlParser: true, useUnifiedTopology: true, });

Line 16: app.post("/product", function (request, response) {

Credits to: @Danizavts and @VedantBang.

Android Dev
  • 35
  • 1
  • 7