MongoDB won't save JSON document? - node.js

Quick question, but does anyone have a clue why MongoDB won't save the following document? I'm using MongoJS in NodeJS to connect to MongoDB and using the following lines of code to save (in my JS file, the function call is underneath the variable definition). The save function doesn't save, but automatically goes to the callback function.
Any thoughts? Thanks so much :).
Edit:
After adding an error log to the callback, I'm getting the following error:
{ [MongoError: error parsing element 0 of field documents :: caused by :: wrong type for '0' field, expected object, found 0: "{"Word":"Count","read":1,"letter":1,"wh":1,"urging":2,"swift":1,"approval":1,"add'l":1,"lease":1,"space":1,"fayetteville":1,"vamc":1,"vets":2,"care":1..."]
name: 'MongoError',
ok: 0,
errmsg: 'error parsing element 0 of field documents :: caused by :: wrong type for \'0\' field, expected object, found 0: "{"Word":"Count","read":1,"letter":1,"wh":1,"urging":2,"swift":1,"approval":1,"add\'l":1,"lease":1,"space":1,"fayetteville":1,"vamc":1,"vets":2,"care":1..."',
code: 9 }
db.collection.save(json_buffer, function() {
console.log("Complete");
});
var json_buffer = {"Word":"Count","read":1,"letter":1,"wh":1,"urging":2,"swift":1,"approval":1,"add'l":1,"lease":1,"space":1,"fayetteville":1,"vamc":1,"vets":2,"care":1,"#gnip":3,"--":3,"delivering":3,"data":3,"happy":3,"customers":3,"outrageous":1,"france":1,"sell":1,"warships":1,"putin":1,"#senatorkirk":1,"#repkinzinger":1,"#usrepkeating":1,"&amp":5,"urge":1,"strong":1,"action":3,"polling":1,"shows":1,"race":1,"close":1,"pitch":1,"pre-primary":1,"deadline":1,"goal":2,"joining":1,"#teamcavuto":1,"shortly":1,"discuss":1,"can’t":1,"continue":1,"punt":1,"debt":2,"crisis":1,"road":1,"watch":1,"opening":1,"remarks":1,"today’s":1,"senate":1,"committee":1,"hearing":1,"mcdonald":1,"nomination":1,"urged":1,"passage":1,"#summermealsact":2,"yesterday":1,"#monticello":1,"#ny--so":1,"impt":1,"expand":1,"#usda's":1,"summer":2,"nutrition":1,"program":1,"great":2,"catch":1,"high":1,"school":2,"friend":1,"john":1,"choate":1,"today":7,"family":1,"capitol":1,"hill":1,"child":1,"america":1,"wake":1,"day":1,"wondering":1,"eat":1,"nebraska":1,"communities":1,"access":1,"local":1,"tv":1,"programming":1,"introduced":1,"bill":4,"work":2,"past":1,"time":2,"congress":1,"meaningful":1,"reduce":1,"threat":1,"cyber":1,"attacks":1,"#mercnews":1,"op-ed":1,"fitting":1,"#us_sfrc":1,"passed":1,"#crpd":1,"bob":1,"dole's":1,"bday":1,"#disabilitytreaty":1,"advocate":1,"beginning":1,"#isupportcrpd":1,"senator":1,"mcconnell":2,"co-sponsors":1,"protect":2,"correctional":1,"officers":2,"daily":1,"independent":2,"#ashland":1,"#kentucky":1,"millions":1,"children":2,"recv":1,"free":1,"reduced":1,"meals":1,"year":1,"left":1,"hungry":1,"months":1,"unacceptable":1,"rt":5,"#aterkel":1,"record":1,"phone":1,"#senschumer":1,"…":1,"good":1,"meeting":1,"anne":1,"rung":1,"nominee":1,"#ombpress":1,"office":2,"fed":1,"procurement":1,"policy":1,"cc":1,"#senatehsgac":1,"#federalreserve":1,"divert":1,"leftover":1,"funds":1,"foreclosure":1,"review":1,"hardest":1,"hit":1,"fund":1,"#ri":1,"supports":1,"corrections":1,"prisons":1,"manchester":1,"pine":1,"knot":1,"ashland":1,"inez":1,"lex":1,"miami":1,"valley":1,"well-run":1,"base":1,"makes":1,"wright-patt":1,"#1":1,"airmen":1,"#gopoversight":1,"release":1,"#irs":2,"needed":1,"days":1,"confirm":1,"lerner":1,"hard":2,"drive":1,"crash":2,"provide":1,"support":3,"runaway":1,"homeless":1,"youth":1,"victims":1,"trafficking":1,"#housefloor":1,"tomorrow":1,"glad":1,"signature":1,"industry":1,"#kydistillers":1,"#repandybarr":1,"obama":1,"meant":1,"flexibility":1,"re-elected":1,"thoughts":1,"prayers":1,"affected":1,"fires":1,"tooele":1,"pray":1,"staying":1,"safe":1,"#utpol":1,"hr":1,"passes":1,"house":1,"#repderekkilmer":1,"modernize":1,"labs":1,"fyi":1,"fun":1,"activities":1,"hosted":1,"google":1,"young":1,"inventors":1,"age":1,"13+":1,"choose":1,"projects":1,"virtual":1,"field":1,"trips":1,"joined":1,"nyc":1,"workforce":2,"development":2,"hosting":1,"roundtable":1,"#bronx":1,"failure":1,"disclose":1,"timely":1,"manner":1,"destroyed":1,"critical":1,"evidence":1,"reason":2,"special":1,"prosecutor":1,"26%":1,"texas":1,"live":1,"poverty":1,"#raisethewage":1,"#honorflightact":2,"codify":1,"process":1,"tsa":1,"expedited":1,"dignified":1,"screening":1,"veterans":1,"visiting":1,"war":1,"memorials":1,"humbled":1,"join":2,"medal":1,"honor":1,"recipient":1,"staff":1,"sergeant":1,"ryan":1,"pitts":1,"nashua":1,"ceremony":1,"pentagon":1,"icymi":1,"statement":1,"halbig":1,"burwell":1,"#ecpzachevans":1,"save":1,"date":1,"mt":1,"#shrinersfest":1,"dates":1,"announced":1,"shrinersfest":1,"june":1,"25-28":1,"feat":1,"#blueangels":1,"htt…":1,"unleash":1,"america's":1,"energy":2,"abundance":1,"create":1,"#jobs":1,"economic":1,"growth":1,"affordable":1,"#yes2energy":1,"marks":1,"#100days":1,"nigerian":1,"schoolgirls":1,"kidnapping":1,"forgotten":1,"#bringbackourgirls":1,"pleased":1,"mayor":1,"#rahmemanuel":1,"taking":1,"#fly2ohare":1,"noise":1,"measure":1,"seeks":1,"pay":1,"gratitude":1,"group":1,"americans":1,"make":1,"ultimate":1,"sacrifice":1,"stopped":1,"dutch":1,"embassy":1,"offer":1,"condolences":1,"lost":1,"loved":1,"#mh17":1,"#obamafailures":1,"min":1,"#131":1,"odds":1,"drives":1,"irs":1,"claims":1,"win":1,"fl":1,"lottery":1,"times":1,"#tcot":1}

Struggled with this for a long time, despite reading up here. Actual cause of the issue finally turned out to be that the JSON object I was trying to update was 'undefined'. Try catching the mongo error and printing the entire error object.
The JSON object was being considered as undefined in my case because it was actually an array, not an object. So I tried doing a mongo update on myJsonObject[0] rather than myJsonObject which was actually an array, and this succeeded.
Do a console.log of the stringified json 'object' you are trying to save, and if it's surrounded by square braces, then you know you're dealing with an array, and need to work on its element(s) instead.

I had this error when:
Create JSON object in client code
Send it over http request
Get it from the request body in node code
Send the body content to mongodb
The problem was the object sent was a String, not a JSON object...
Using var json = JSON.parse(json-string-from-body) and send json solve my problem.
Also, I used insert() method not save(). In mongodb console save({"htt…":1}) which is a part of your data return an error but insert({"htt…":1}) works fine.

I got this error with Mongoose when doing
Model.update(docId, {...})
when I should have been doing
Model.update({_id: docId}, {...})

Related

express.json() is not parsing nested json object. it return [object Object] instead

express.json() is not parsing nested json object. it return [object Object] instead.
express - 4.17.1,
Request Headers
Content-Type: application/json;charset=UTF-8
I m using express.json() as follows.
app.use(express.json());
when i post data without nested object. it works
const data = await this.$axios.$post(`user/update/${this.username}`, {confirmationCode: "code"});
now req.body.confirmationCode is "code".
but if i post nested json object. It doesn't works
const data = await this.$axios.$post(`user/update/${this.username}`, {confirmationCode: { mobile: "code"}});
now req.body.confirmationCode.mobile is undefined.
and req.body.confirmationCode is [object Object].
I noticed that several people have downvoted this without actually answering the issue. I too had this as a problem and it turns out to be a simple fix. Try passing the object through JSON.stringify() and see if that helps.
example code:
`console.log(`My Test req.body: ${JSON.stringify(req.body)} \n`);`
Also, if you are passing the JSON as an argument to a function, do not attempt to pass it plain text (in string format). Instead use it unadulterated as I did, req.body, or create a new object and send the variable of the object instead of the string version.

Get user_likes and user_posts from Facebook using NodeJS

I need to develop an App that get some information by FB (user_posts and user_likes) I'm trying implement its using Passport-facebook.
I tried, (just 'likes' to test first)
newUser.facebook.likes = profile.likes[0].value;
and the error is:
TypeError: Cannot read property '0' of undefined
'likes' is on my profileFields.
I put my code that is working on my Github. (whitout likes and posts)
Other information like (id, name, email...) works very well, but I'm not be able to get the data user_likes and user_posts. I would like to enter this data in my MongoDB database.
The method that follow (in Javascript):
function getInfo() {
FB.api('me', {fields :'id,name,posts,likes'}, function(response) {
var dataStr = "data:application/octet-stream;charset=utf-8," + encodeURIComponent(JSON.stringify(response));
var dlAnchorElem = document.getElementById('downloadAnchorElem');
dlAnchorElem.setAttribute("href", dataStr);
dlAnchorElem.setAttribute("download", "feed.json");
dlAnchorElem.click();
});
};
Do what I want, but it writes data (.json) in a file, I want to record in the database and using NodeJS.
Any help?
Are you aware of Mongoose and how to define a schema in mongoose.
i suggest that it would be the simplest way for you to do.
you can use json-schema-to-mongoose (see link below for example)
https://www.npmjs.com/package/json-schema-to-mongoose
and them simply save the document to mongodb.

Error while getting parameters from request object containing `&` character

I'm trying to get param from request object using following code.
module.exports = function (req, res) {
var query = req.query;
var data = JSON.parse(query.param1);
}
This is working fine for most of the cases.
If param1 contains & character then query.param1 get values before & and next values are considered as new param.
eg
localhost/?param1={"url":"http://s.test.com/x?format=jsonp&id=a&callback=b"}
Edit original url is already encoded
localhost/?param1=%7B%22url%22%3A%22http%3A%2F%2Fs.test.com%2Fx%3Fformat%3Djsonp%26id%3Da%26callback%3Db%22%7D
in this case I'm getting param1 = {"url":"http://s.test.com/x?format=jsonp
which is not valid json, so I'm getting error, currently we've solved it using regex (removing localhost/?param1= part of url).
What's best way to handle this use case?
Edit : server environment
centos 6.5 server
node v0.12.7
express#4.13.3
req.query is already an object with express :
An object containing a property for each query string parameter in the route. If there is no query string, it is the empty object, {}.
req.query documentation
You have to encode your URL before calling nodejs API. And on the nodejs side, you have to decode the URL to get correct parameteres.

TypeError: Object #<Promise> has no method 'forEach' mongodb

I am trying to copy a document from one collection to another using MongoDB and nodejs in visual studio. Here's what I am trying to do:
var newcollection = db.get('mynewdata');
var oldcollection = db.get('myusercollection');
var userName = req.body.username;
oldcollection.find({ "username" : userName }).forEach(function (docs) {
newcollection.insert(docs);
});
On the Mongo website it says that collection.find() should return a cursor which should be able to use the forEach() method. However, it's not returning a cursor object and forEach doesn't seem to be defined for whatever its returning.
I get the error from the subject line that says: TypeError: Object #<Promise> has no method 'forEach'
Any ideas as to why it is giving me this error?
Try this:
oldcollection.find({ "username" : userName }).exec(function(err, results){
if(err){
throw err
} else {
results.forEach(function(docs){
newcollection.insert(docs)
})
}
});
I think your problem is the response from the query is an object. This method works for me.
On the Mongo website
That may be the case with the core mongo API, but not the Node.js module.
But also, make sure you're using the right version of the Node library (and the official one too).

Errors when trying NetSuite's Restlet interface: datain is null

I'm trying out Restlets for the first time and I am having trouble returning any data.
I've built a basic restlet and deployed it in NetSuite. The code is as follows:
function getRESTlet(dataIn) {
return nlapiLoadRecord(dataIn.recordtype, dataIn.id); // e.g recordtype="customer", id="769"
}
Using Chromes' REST Console application I've set up the following:
Request URI: https://rest.netsuite.com/app/site/hosting/restlet.nl?script=123&deploy=1&recordtype=customer&id=2409
Request Headers: NLAuth nlauth_account=123456,nlauth_email=email#emailaddy.com,nlauth_signature=password
Running it as a GET operation I return the following error:
error code: JS_EXCEPTION
error message:type
Followed by the following email:
Date & Time: 8/19/2013 2:48 pm
Execution Time: 0.06s
Script Usage: 0
Script: getRecord
Type: RESTlet
Function: getRESTlet
Error: SSS_MISSING_REQD_ARGUMENT
type
Stack Trace: getRESTlet(getCustomer.js:14)
restletwrapper(null$lib:3)
The customer record exists, the RestLet code is from the NetSuite help system, and I get an error email when the script fails so I know the deployment URL is good.
Any ideas on what I'm doing wrong?
--EDIT--
Changing my function signature to function getRESTlet(type,dataIn) resolves the type error, although now I get:
error code: UNEXPECTED_ERROR
error message:TypeError: Cannot read property "recordtype" from undefined (getCustomer.js#14)
I've also removed customer and id from the request URI and instead listed them in the Request Payload section of REST Console.
--Edit 2--
adding nlapiLogExecution('DEBUG', 'JSON.stringify(datain) is:', JSON.stringify(datain)); to the function is returning blank for the log entry. It appears that datain is null....
I had a similar issue that was resolved once I set the request header content-type to application/json.
The default content-type is text/plain which passes a stringified JSON object to the function and expects a string to be returned.
If you set the content-type to application/json or application/xml the 'datain' parameter will come in as a javascript object.
From the NetSuite Docs:
If users specify a content type other than JSON or TEXT, a 415 error
is returned with the following message:
Invalid content type. You can
only use application/json, application/xml or text/plain with
RESTlets.
If users provide data in a format different from specified
type, the following error is returned with one of the following
messages:
Error code = INVALID_RETURN_DATA_FORMAT Error message =
Invalid data format. You should return TEXT. Error message = Invalid
data format. You should return a JavaScript object.
To fix the error of
Error code = INVALID_RETURN_DATA_FORMAT Error message =
Invalid data format. You should return TEXT. Error message = Invalid
data format. You should return a JavaScript object.
You can supply a Content-type to the get specified get request of 'json'. Documentation would seem to indicate that you should specify "accepts" json, but for me only the content-type on a get made the request work.
I got this error: SSS_MISSING_REQD_ARGUMENT when trying to submit a transform record type that I had specified in camel case rather that all lower case... took me a day to work that out :(
changing the function to:
function getRESTlet(datain) {
return nlapiLoadRecord(datain.recordtype, datain.id); // e.g recordtype="customer", id="769"
}
resolves the issue. I thought I had tried without camelCase, but alas I did not.
So moral of the story is: datain is case sensitive!

Resources