Unable to send request to MongoDB nodeJS ADD through POSTMAN - - node.js

The question is pretty much self explanatory. I have created a backend for MongoDB using nodeJS. Here is the link to my entire backend project so you can replicate at your own end -->
https://onedrive.live.com/?cid=90b44987ff0f70ac&id=90B44987FF0F70AC%21157&authkey=!AOG8D2nRT--ga9I
Now, through Postman I am trying to send requests to the add operation to add new player items -
The steps I am following are -->
starting up mongo.exe and running use playerDB
starting up mongod.exe
opening command terminal on my backend project and running nodemon server
sending a POST request to Mongoose db through postman like
localhost:4000/playerDB/add
The error I am receiving is ->
400 Unable to add player object
My sample request body is like so:
{
"player_name":"Didier Drogba",
"player_description":"Brawny, cunning, determined and a heart for attempting the most impossible audacious! The one and only!",
"player_position":"Striker",
"player_age":"42",
"player_club":"Chelsea FC",
"player_":"Hall of Fame",
"player_isactive":"False",
"player_completed":"True"
}
What error did I make? What was the problem? Anythign wrong with the sequence of steps I followed?
Can you point out where I went wrong?
Thanks,
EDIT-->
The new error is:
<body>
<pre>SyntaxError: Unexpected token F in JSON at position 288
<br> at JSON.parse (<anonymous>)
<br> at parse (E:\REACT\MyProj\backend\node_modules\body-parser\lib\types\json.js:89:19)
<br> at E:\REACT\MyProj\backend\node_modules\body-parser\lib\read.js:121:18
<br> at invokeCallback (E:\REACT\MyProj\backend\node_modules\raw-body\index.js:224:16) .....

I have seen your code and there were a couple of changes that were needed.
1) In your player.model.js file, the module.exports should be at the end of the file.
This is because you are exporting the player schema even before it is declared and defined.
2) Two properties in the model: a) player_isactive and b) player_completed are defined as Boolean and you were passing a string from the postman. So you need to send the data as follows:
{
"player_name":"Didier Drogba",
"player_description":"Brawny, cunning, determined and a heart for attempting the most impossible audacious! The one and only!",
"player_position":"Striker",
"player_age":"42",
"player_club":"Chelsea FC",
"player_":"Hall of Fame",
"player_isactive": false,
"player_completed":true
}
Kindly make this changes and your problem is solved.

Related

Acces JSON Object in google cloud functions

i'm trying to parse a response in a google cloud function.
The text response is:
{"data":{"Object_Create":{"id":107}}} and stored in var body.
i convert it this way:
var obj= JSON.parse(body)
Then i try to access the object:
var Object_Create= obj.data.Object_Create
and works.
The problem is when i try to get the id field with:
var id= obj.data.Object_Create.id
because it returns ReferenceError: id is not defined
I tried the same code from an online JS editor and it's working without any problems so it seems related to the cloud platform.
Did someone experienced the same problem?
There is no need to parse that response, as it is a Javascript object, which means there is no extra processing necessary.
If you do try to parse it, it should throw a syntax error
SyntaxError: Unexpected token o in JSON at position 1
you can just access it by
console.log(body.data.Object_Create.id)
I imagine you actually want to use that key however, so you could
const { Object_Create } = body.data
console.log(Object_Create.id)

How do I init XOD in WebViewer? "DisplayModes" is undefined

I'm trying to load a XOD document into a PDFTron WebViewer. As far as I can read in the documentation and samples, this should be a simple "plug and play"-operation - it should simply work when you point at a file. Ideally, in my example, the document should be fetched from a service, as so:
fetch('/myservice/GetXOD')
.then(function(data) {
$(function() {
var viewerElement = document.getElementById("viewer");
var myWebViewer = new PDFTron.WebViewer({
initialDoc: data.body
}, viewerElement);
});
});
Unfortunately I get the following error:
Uncaught TypeError: Cannot read property 'DisplayModes' of undefined
The reason I'm doing it in a fetch, is because I'm rendering a Handlebars template, and pass the data to instantiate in a callback. However, I've isolated the code into an otherwise "empty" HTML-document, and in the simplified example below, I'm simply pointing at the XOD provided by PDFTron on page load (no fetch this time).
$(function() {
var viewerElement = document.getElementById("viewer");
var myWebViewer = new PDFTron.WebViewer({
initialDoc: 'GettingStarted.xod' // Using the XOD provided by PDFTron
}, viewerElement);
});
This unfortunately returns a different error (HTTP status 416).
Uncaught Error: Error loading document: Error retrieving file: /doc/WebViewer_Developer_Guide.xod?_=-22,. Received return status 416.
The same error appears when I run the samples from PDFTron on localhost.
I'm at a complete loss of how I should debug this further - all the samples assume everything is working out of the box.
I should note that I can actually get PDFs working just fine on localhost, but not on the server. XODs are problematic both on the server and on localhost.
I'm sorry to hear you are having troubles running our samples.
Your error message says 416 which means "Requested range not satisfiable". Perhaps your development servers do not support byte range requests (https://en.wikipedia.org/wiki/Byte_serving).
Could you try passing an option streaming: true? When streaming is true you're just requesting the entire file up front which shouldn't be a problem for any servers but it is a problem for WebViewer if the file is large because it will need to be completely downloaded and parsed at the start which is slow.

Can't connect to MongoDB database with NodeJS native driver

I have a NodeJS app in which I need to connect to to MongoDB databases - one a single server set up, and the second from a replica set. I connect to the next one just fine, but when connecting to the second one - I get the following error:
/Users/iddogino/Documents/RapidApp/node_modules/mongodb/lib/mongodb/db.js:299
throw err;
^
TypeError: Cannot set property 'auto_reconnect' of undefined
at /Users/iddogino/Documents/RapidApp/node_modules/mongodb/lib/mongodb/connection/repl_set/options.js:110:35
at Array.forEach (native)
at Options.decorateAndClean (/Users/iddogino/Documents/RapidApp/node_modules/mongodb/lib/mongodb/connection/repl_set/options.js:108:16)
at new exports.ReplSet (/Users/iddogino/Documents/RapidApp/node_modules/mongodb/lib/mongodb/connection/repl_set/repl_set.js:84:31)
at /Users/iddogino/Documents/RapidApp/node_modules/mongodb/lib/mongodb/mongo_client.js:320:30
at /Users/iddogino/Documents/RapidApp/node_modules/mongodb/lib/mongodb/db.js:296:11
at process._tickDomainCallback (node.js:459:13)
The code I use to connect (after omitting user names and real urls) is:
require('mongodb').MongoClient.connect("mongodb://password#url1:port1,url2:port2/dbName?replicaSet=setName&w=0&readPreference=secondary", function(err, doc) {...});
Now when I tried this alone (not after the code connecting to true other DB), I worked just fine... Any ideas?
This one took me a minute to figure out. The error says the problem is in ./node_modules/mongodb/lib/mongodb/connection/repl_set/options.js:110
The issue is that options.js:91 creates an empty object. They're doing this make a dictionary and deduplicate the 'host:port' strings for the servers. options.js:104 loops through the keys in that dictionary and blindly loads them into an array. This would be a problem if you have added something to Object.prototype globally since it would also get added to the final array of servers. Since whatever you've added to Object.prototype probably isn't a server, it won't have an options property and you'll get this error.
Work around:
Figure out where in your code you have modified Object.prototype and make it less general. I think they've updated this in newer versions of the driver, but if you're using an old one you need to work around it.

Frisby.JS - unexpected token ( when sending a POST with json

I'm getting the following error,
Error: Error parsing JSON string: Unexpected token <
Given: <html>
<body>
<p>
<b>400.</b> Invalid json request: numOfDays=1&checkInDate=2013-04-10.
Make sure that key names and text values are quoted with "
</p>
</body>
</html>
Stacktrace:
using the following code:
var frisby = require('frisby');
frisby.create('Ensure proper values in specified keys')
.post('http://localhost:9090/bookRoom', {"numOfDays": 1 , "checkInDate":"2013-04-10"})
.expectJSON({
"checkInDate": "2013-04-10",
"checkOutDate": "2013-04-11",
"totalPrice": 130
})
.toss()
I'm trying to pass this JSON - {"numOfDays": 1 , "checkInDate":"2013-04-10"}
EDIT:
It seems that the JSON being passed is in the following format. How do I get rid of those backslashes?
Handling booking request: "{\"numOfDays\":\"1\",\"checkInDate\":\"2013-04-10\"}"
Author of Frisby.js here. Please update to Frisby v0.8.5 - this is due to a change in the "request" module that Frisby depends on and uses.
I've just run into the same problem but I used different parameters for post. Precisely I added {json: true} as 3rd parameter and after a few minutes of struggling I removed it and it worked.
It looks your case might be the other way around. Try to put that 3rd parameter in the post method call and hope the best.
Related documentation: API docs

Appending document in response function

Not sure why, but I'm getting an unexpected identifier error when trying to append an element to the document in a response function. I've found that doing anything with the document seems to give me this error. Here's a sample code:
chrome.extension.sendRequest({send:data},function(response) {
document.body.innerHTML='test'
})
It looks to me like it should work, but evidently it does now. This piece of code is located in the contentscript, and messing with the document outside of this function seems to work just fine, but I always get "unexpected identifier" when trying this. Unfortunately I cannot do it outside of the function because the response determines whether or not an element is added to the body.
The code you shared should work. Try restarting your browser to see if that fixes it.

Resources