I am able to send data to db using the following:
$("#set").click(function ()
{
var cells = graph.getCell();
var StateObject = graph.toJSON();
var SavedState = JSON.stringify(StateObject);
console.log("Here is the state object " + StateObject);
console.log("Here is the saved state: " + SavedState);
$.ajax({
url: 'JsonProcessor.do',
type: 'post',
dataType: 'json',
data: {
test: StateObject
}
});
});
But while fetching I get error in chrome console like this:
Uncaught Error: Graph JSON must contain cells array
Code to fetch data:
$("#get").click(function ()
{
$.ajax({
url: 'JsonProcessor.do',
type: 'get',
dataType: 'json',
success: function (data) {
var result = data;
console.log("Result from Database: " + result);
graph.fromJSON(result);
}
});
});
I can see the object fetched in chrome console like this: [object Object] and when I expand it
`0: Object
JSON_Diagram: "test=%5B%7B%22empID%22%3A%22%22%7D%2C%7B%22cells%22%3A%5B%7B%22type%22%3A%22basic.Rect%22%2C%22position%22%3A%7B%22x%22%3A-2%2C%22y%22%3A33%7D%2C%22size%22%3A%7B%22width%22%3A71%2C%22height%22%3A625%7D%2C%22angle%22%3A0%2C%22isInteractive%22%3Afalse%2C%22id%22%3A%22ca4cc8a8-7e95-43cb-a8d3-8cf24c82d43f%22%2C%22z%22%3A1%2C%22embeds%22%3A%5B%22b6aee295-16fa-41e7-b25e-d20610cbd631%22%2C%2288de9a15-cbc8-4c02-a358-30491d50cb37%22%5D%2C%22attrs%22%3A%7B%22rect%22%3A%7B%22fill%22%3A%22%23EEEEEE%22%2C%22stroke%22%3A%22%23008B8B%22%2C%22stroke-width%22%3A2%7D%2C%22.%22%3A%7B%22magnet%22%3Afalse%7D%7D%7D%2C%7B%22type%22%3A%22basic.Circle%22%2C%22size%22%3A%7B%22width%22%3A53%2C%22height%22%3A53%7D%2C%22position%22%3A%7B%22x%22%3A8%2C%22y%22%3A130%7D%2C%22angle%22%3A0%2C%22isInteractive%22%3Afalse%2C%22id%22%3A%22b145b765-a60b-4742-82a6-712da89e4dd0%22%2C%22z%22%3A3%2C%22attrs%22%3A%7B%22.%22%3A%7B%22magnet%22%3Afalse%7D%2C%22circle1%22%3A%7B%22fill%22%3A%22white%22%2C%22stroke-width%22%3A2%2C%22stroke%22%3A%22green%22%7D%7D%7D%2C%7B%22type%22%3A%22basic.Circle%22%2C%22size%22%3A%7B%22width%22%3A53%2C%22height%22%3A53%7D%2C%22position%22%3A%7B%22x%22%3A8%2C%22y%22%3A225%7D%2C%22angle%22%3A0%2C%22isInteractive%22%3Afalse%2C%22id%22%3A%221b99b595-57cd-4127-9ec0-d5842beb183a%22%2C%22z%22%3A4%2C%22attrs%22%3A%7B%22.%22%3A%7B%22magnet%22%3Afalse%7D%2C%22circle2%22%3A%7B%22fill%22%3A%22white%22%2C%22stroke%22%3A%22green%22%7D%7D%7D%2C%7B%22type%22%3A%22basic.Circle%22%2C%22size%22%3A%7B%22width%22%3A53%2C%22height%22%3A53%7D%2C%22position%22%3A%7B%22x%22%3A8%2C%22y%22%3A320%7D%2C%22angle%22%3A0%2C%22isInteractive%22%3Afalse%2C%22id%22%3A%227e953c11-d361-44d0-bf29-3d3fa146519c%22%2C%22z%22%3A5%2C%22attrs%22%3A%7B%22.%22%3A%7B%22magnet%22%3Afalse%7D%2C%22circle3%22%3A%7B%22fill%22%3A%22white%22%2C%22stroke%22%3A%22green%22%7D%7D%7D%2C%7B%22type%22%3A%22basic.Rect%22%2C%22position%22%3A%7B%22x%22%3A35%2C%22y%22%3A505%7D%2C%22size%22%3A%7B%22width%22%3A55%2C%22height%22%3A55%7D%2C%22angle%22%3A0%2C%22isInteractive%22%3Afalse%2C%22id%22%3A%22186a6dd7-2b5e-48d6-8e35-ee821ebbb4dc%22%2C%22z%22%3A7%2C%22attrs%22%3A%7B%22rect%22%3A%7B%22fill%22%3A%22%23FFED6B%22%2C%22stroke%22%3A%22%23DBCB62%22%2C%22width%22%3A55%2C%22height%22%3A55%2C%22stroke-width%22%3A1%2C%22transform%22%3A%22rotate(45)%22%7D%2C%22.%22%3A%7B%22magnet%22%3Afalse%7D%7D%7D%2C%7B%22type%22%3A%22basic.Rect%22%2C%22position%22%3A%7B%22x%22%3A10%2C%22y%22%3A50%7D%2C%22size%22%3A%7B%22width%22%3A51%2C%22height%22%3A41%7D%2C%22angle%22%3A0%2C%22isInteractive%22%3Afalse%2C%22id%22%3A%22b6aee295-16fa-41e7-b25e-d20610cbd631%22%2C%22z%22%3A9%2C%22parent%22%3A%22ca4cc8a8-7e95-43cb-a8d3-8cf24c82d43f%22%2C%22attrs%22%3A%7B%22rect%22%3A%7B%22fill%22%3A%22%23D6F2FC%22%2C%22stroke%22%3A%22%237E7E7E%22%7D%2C%22.%22%3A%7B%22magnet%22%3Afalse%7D%7D%7D%2C%7B%22type%22%3A%22basic.Rect%22%2C%22position%22%3A%7B%22x%22%3A10%2C%22y%22%3A420%7D%2C%22size%22%3A%7B%22width%22%3A51%2C%22height%22%3A41%7D%2C%22angle%22%3A0%2C%22isInteractive%22%3Afalse%2C%22id%22%3A%2288de9a15-cbc8-4c02-a358-30491d50cb37%22%2C%22z%22%3A12%2C%22parent%22%3A%22ca4cc8a8-7e95-43cb-a8d3-8cf24c82d43f%22%2C%22attrs%22%3A%7B%22.%22%3A%7B%22magnet%22%3Afalse%7D%2C%22rectGroup0%22%3A%7B%22fill%22%3A%22white%22%2C%22stroke%22%3A%22%237E7E7E%22%7D%7D%7D%2C%7B%22type%22%3A%22devs.Model%22%2C%22size%22%3A%7B%22width%22%3A751%2C%22height%22%3A170%7D%2C%22inPorts%22%3A%5B%5D%2C%22outPorts%22%3A%5B%5D%2C%22position%22%3A%7B%22x%22%3A160%2C%22y%22%3A123%7D%2C%22angle%22%3A0%2C%22id%22%3A%2237a88170-4da8-464d-99f9-32b72097c495%22%2C%22z%22%3A14%2C%22embeds%22%3A%5B%22e70977f4-52b0-4fdb-a852-8b3b63a5273e%22%5D%2C%22attrs%22%3A%7B%22.label%22%3A%7B%22text%22%3A%22CONTAINER%22%2C%22ref-y%22%3A0.1%2C%22y-alignment%22%3A%22middle%22%7D%2C%22rect%22%3A%7B%22fill%22%3A%22%22%2C%22opacity%22%3A%220.60%22%7D%7D%7D%2C%7B%22type%22%3A%22devs.Model%22%2C%22size%22%3A%7B%22width%22%3A51%2C%22height%22%3A41%7D%2C%22inPorts%22%3A%5B%22%22%5D%2C%22outPorts%22%3A%5B%22%22%5D%2C%22position%22%3A%7B%22x%22%3A234%2C%22y%22%3A187%7D%2C%22angle%22%3A0%2C%22id%22%3A%22e70977f4-52b0-4fdb-a852-8b3b63a5273e%22%2C%22z%22%3A15%2C%22parent%22%3A%2237a88170-4da8-464d-99f9-32b72097c495%22%2C%22attrs%22%3A%7B%22.%22%3A%7B%22magnet%22%3Atrue%7D%2C%22.port-body%22%3A%7B%22r%22%3A3%7D%2C%22.label%22%3A%7B%22text%22%3A%22%22%2C%22ref-x%22%3A0.4%2C%22ref-y%22%3A0.2%7D%2C%22rect%22%3A%7B%22fill%22%3A%22%23D6F2FC%22%2C%22stroke%22%3A%22%237E7E7E%22%7D%2C%22.inPorts+circle%22%3A%7B%22type%22%3A%22input%22%7D%2C%22.outPorts+circle%22%3A%7B%22type%22%3A%22output%22%7D%2C%22.inPorts%3E.port0%3E.port-label%22%3A%7B%22text%22%3A%22%22%7D%2C%22.inPorts%3E.port0%3E.port-body%22%3A%7B%22port%22%3A%7B%22id%22%3A%22in29%22%2C%22type%22%3A%22in%22%7D%7D%2C%22.inPorts%3E.port0%22%3A%7B%22ref%22%3A%22.body%22%2C%22ref-y%22%3A0.5%7D%2C%22.outPorts%3E.port0%3E.port-label%22%3A%7B%22text%22%3A%22%22%7D%2C%22.outPorts%3E.port0%3E.port-body%22%3A%7B%22port%22%3A%7B%22id%22%3A%22out30%22%2C%22type%22%3A%22out%22%7D%7D%2C%22.outPorts%3E.port0%22%3A%7B%22ref%22%3A%22.body%22%2C%22ref-y%22%3A0.5%2C%22ref-dx%22%3A0%7D%7D%7D%5D%7D%5D"
__proto__: Object
1: Object
2: Object
3: Object
length: 1
__proto__: Array[0]`
but problem is rendering the data on to view in jointjs
Please help if any body have already worked it out.
A key called JSON_Diagram is appended to json array when retrieving it. Json sent via ajax call must return exact json for the graph to render the diagram. You need to fix your JsonProcessor.do servlet to return same json. Hope this helps.
JointJS' fromJSON expects an input of the format { cells: [...] }. So I suspect that if you're saving your JSON using { test: JSON.stringify } you will need to JSON.parse(result.data) and then pass it to graph.fromJSON.
Final solution
graph.fromJSON(JSON.parse(result.data))
Related
I am pulling my hairs off. On http request, i can print a body object but can not access its content.
So i am sending a request like this from web:
return fetch('https:xxxxxxx/xxxx', {
method: 'post',
body: JSON.stringify(saleObject),
headers: {
'Accept': 'application/json',
'contentType':"application/json",
'dataType':"json",
}
}).then(function(res) {
return res.json();
}).then(function(data) {
return data.orderID;
});
}
On my server (nodejs express Firebase ) , I am trying to read it in multiple ways :
exports.payPalIntent = functions.https.onRequest(async(req, res) => {
return cors(req, res, async () => {
console.log("req.body",req.body); //this print AN OBJECT, A REAL OBJECT NOT A STRING
console.log("req address",req.body.address); //=undefined, there is address property inside which
return res.send(200);
});
So, the first one print this :
req.body {"address":{"city":"some city","zip":"345334","area":"USA","street":"Hai 13", ........
The second says Cannot read property 'address' of undefined
Of course, i tried also JSON.stringify(req.body) , which print a string object that i can't access.
How do you access this object ?
JSON.stringify turns a JavaScript object into a JSON string. You already have a JSON string and you want to covert it to a JavaScript object, so use the reverse: JSON.parse(req.body). Then you should be able to access the properties using the dot operator.
I'm trying to use frisby.js to specify an API test for an endpoint that returns a plain text response to a valid POST request. However I'm having problems getting frysby.js to accept non-JSON response documents. Whenever a response returns non-JSON content, throw a TypeError due to 'Unexpected token b in JSON at position 0'.
As an example, I'm sending a HTTP POST request with the JSON document shown below is expected to return a response with a plaintext document with the string bar.
{
"foo":{
"name":"bar"
}
}
Here's the unit test that I've wrote to verify the response:
it('should create a foo resource', function () {
return frisby.post('http://localhost:8080/',
{
"foo":{
"name":"bar"
}
})
.expect('status',201);
});
Unfortunately, the following error is thrown by frisby.js when I run the test:
FAIL ./test.js
✕ should create a foo resource (17ms)
● should create a foo resource
TypeError: Invalid json response body: 'bar' at http://localhost:8080/ reason: 'Unexpected token b in JSON at position 0'
Does anyone know if it's possible to configure each test to expect some data format other than JSON?
If you getting JSON + something then break jsonTypes in two format like one for JSON object and one for other, like it having array in JSON objects.
and then put expect conditions on them.
This might helps you:
const frisby = require('frisby');
const Joi = frisby.Joi;
frisby.globalSetup({
headers : {
"Accept": "application/json",
"content-type" : "application/json",
}
});
it("should create a foo resource", function () {
frisby.post("http://localhost:8080/")
.expect("status", 200)
.expect("header", "content-type", "application/json; charset=utf-8")
.expect("jsonTypes", "data.foo", {
"name": Joi.string()
})
.then(function(res) { // res = FrisbyResponse object
var body = res.body;
body = JSON.parse(body);
expect(body.data.foo.name).toBeDefined();
})
});
I have these two functions:
function menuItemListener( link ) {
var side = link.getAttribute("data-action");
if (side == 'Mark as A' || side == 'Mark as B') {
highlighter(side);
$.ajax({
method: "POST",
url: "http://localhost:3000/",
dataType: "json",
data: JSON.stringify({"rangyobject" : rangy.saveSelection()}),
contentType: "application/json; charset=utf-8",
success: function(result) {
console.log('yei');
}
});
}
toggleMenuOff();
}
function loadHighlights () {
window.addEventListener("load", function load(event){
$.ajax({
method: "GET",
url: "http://localhost:3000/ranges",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(result) {
console.log('loadHighlights results: ', result[0].rangyObject);
rangy.restoreSelection(result[0].rangyObject);
}
});
},false);
};
So it's simply storing the rangy.saveSelection() in the DB and then getting it back and trying to rangy.restoreSelection on the its first element.
the console.log looks good but I'm getting an error in the console:
Error: Error in Rangy WrappedRange module: createRange(): Parameter must be a Window object or DOM node
rangy.saveSelection() looks like this in the console:
{win: undefined, rangeInfos: Array, restored: false}
Note the "win: undefined"
When I send it to the DB, I use
data: JSON.stringify({"rangyObject" : rangy.saveSelection()})
When retrieving it back from the DB it looks like:
{rangeInfos: Array, restored: false}
Notice that the 'win' was omitted by the save to the db
I was able to use the serialize and deserialize functions to circumvent this issue, but these don't create the id's with the span's they create, so I ran into another problem.
I'm a bit of a noob working on my first CRUD application (dream journal) using Node, Express, and MongoDB/Mongoose.
I'm creating a simple object based on a few user form inputs and then submitting a post request to the server, which in turn calls Mongoose.save().
The object I'm creating on the client side, however, is slightly different than the one that is being received by the server, and I'm at a complete loss as to why.
First I'm getting some values from a form and putting them in an object:
// within button click event handler
var dreamText = $('#dream-text').val().trim();
var dreamTags = ["tag 1", "tag 2", "tag 3"]; // for debugging
var dreamLucid = $('#dream-lucid').val() == 'Yes' ? true : false;
var dreamDate = $('#dream-date').val();
var dream = {
userID: 'test',
text: dreamText,
tags: dreamTags,
isLucid: dreamLucid,
dateCreated: dreamDate
};
Here a console.log of dream shows everything as it should be:
Object {userID: "test", text: "sample text", tags: Array[3], isLucid: false, dateCreated: "2016-08-08"}
From there I'm submitting a POST request with the dream object:
$.ajax({
type: 'POST',
url: '/new',
data: dream,
success: function(data){
console.log('dream saved');
}
});
Now on the server side, with Express and Mongoose, I'm attempting to save the object to a DB:
router.post('/', urlencodedParser, function(req, res) {
console.log(req.body);
var newDream = Dream(req.body).save(function(err, data){
if (err) {
throw error;
}
});
});
All the properties except tags are saving correctly, and a console.log of req.body yields:
{ userID: 'test',
text: 'sample text',
'tags[]': [ 'tag 1', 'tag 2', 'tag 3' ],
isLucid: 'false',
dateCreated: '2016-08-08' }
Any idea why the tags property on the client side is becoming 'tags[ ]' when it gets passed through the POST request?
I've tried both tags: Array and tags: [String] in my Mongoose Model, but the problem seems to be occurring before I ever call Mongoose.save.
By default, $.ajax() encodes a POST operation as application/x-www-form-urlencoded which does different things with arrays. You probably just want to send JSON so you can receive JSON on the server.
You can cause that to happen by manually turning your data into JSON and setting the content type appropriately:
$.ajax({
type: 'POST',
url: '/new',
data: JSON.stringify(dream),
contentType: "application/json",
success: function(data){
console.log('dream saved');
}
});
Then, you may also have to parse the JSON on the server side of things, either manually for this request or with some middleware like bodyParser.json().
I'm simply trying to create a node server that outputs the HTTP status of a given URL.
When I try to flush the response with res.write, I get the error: throw new TypeError('first argument must be a string or Buffer');
But if I replace them with console.log, everything is fine (but I need to write them to the browser not the console).
The code is
var server = http.createServer(function (req, res) {
res.writeHead(200, {"Content-Type": "text/plain"});
request({
uri: 'http://www.google.com',
method: 'GET',
maxRedirects:3
}, function(error, response, body) {
if (!error) {
res.write(response.statusCode);
} else {
//response.end(error);
res.write(error);
}
});
res.end();
});
server.listen(9999);
I believe I should add a callback somewhere but pretty confused and any help is appreciated.
I get this error message and it mentions options.body
I had this originally
request.post({
url: apiServerBaseUrl + '/v1/verify',
body: {
email: req.user.email
}
});
I changed it to this:
request.post({
url: apiServerBaseUrl + '/v1/verify',
body: JSON.stringify({
email: req.user.email
})
});
and it seems to work now without the error message...seems like bug though. I think this is the more official way to do it:
request.post({
url: apiServerBaseUrl + '/v1/verify',
json: true,
body: {
email: req.user.email
}
});
response.statusCode is a number, e.g. response.statusCode === 200, not '200'. As the error message says, write expects a string or Buffer object, so you must convert it.
res.write(response.statusCode.toString());
You are also correct about your callback comment though. res.end(); should be inside the callback, just below your write calls.
Request takes a callback method, its async! So I am assuming, by the time the callback is executed the res.end() might get called. Try closing the request within the callback.
Well, obviously you are trying to send something which is not a string or buffer. :) It works with console, because console accepts anything. Simple example:
var obj = { test : "test" };
console.log( obj ); // works
res.write( obj ); // fails
One way to convert anything to string is to do that:
res.write( "" + obj );
whenever you are trying to send something. The other way is to call .toString() method:
res.write( obj.toString( ) );
Note that it still might not be what you are looking for. You should always pass strings/buffers to .write without such tricks.
As a side note: I assume that request is a asynchronous operation. If that's the case, then res.end(); will be called before any writing, i.e. any writing will fail anyway ( because the connection will be closed at that point ). Move that line into the handler:
request({
uri: 'http://www.google.com',
method: 'GET',
maxRedirects:3
}, function(error, response, body) {
if (!error) {
res.write(response.statusCode);
} else {
//response.end(error);
res.write(error);
}
res.end( );
});
if u want to write a JSON object to the response then change the header content type to application/json
response.writeHead(200, {"Content-Type": "application/json"});
var d = new Date(parseURL.query.iso);
var postData = {
"hour" : d.getHours(),
"minute" : d.getMinutes(),
"second" : d.getSeconds()
}
response.write(postData)
response.end();
And there is another possibility (not in this case) when working with ajax(XMLhttpRequest), while sending information back to the client end you should use res.send(responsetext) instead of res.end(responsetext)
Although the question is solved, sharing knowledge for clarification of the correct meaning of the error.
The error says that the parameter needed to the concerned breaking function is not in the required format i.e. string or Buffer
The solution is to change the parameter to string
breakingFunction(JSON.stringify(offendingParameter), ... other params...);
or buffer
breakingFunction(BSON.serialize(offendingParameter), ... other params...);
The first argument must be one of type string or Buffer. Received type object
at write_
I was getting like the above error while I passing body data to the request module.
I have passed another parameter that is JSON: true and its working.
var option={
url:"https://myfirstwebsite/v1/appdata",
json:true,
body:{name:'xyz',age:30},
headers://my credential
}
rp(option)
.then((res)=>{
res.send({response:res});})
.catch((error)=>{
res.send({response:error});})