Page 1 of 2

Insert records on success event

Posted: Mon Feb 17, 2014 5:33 am
by Ralph Ngo

I've tried to invoke a create service in Javascript but it failed. Following is my code:

precodevar response = JSON.parse(localStorage.getItem("json_response"));
for (var i=0; i<response&#46;data&#46;length; i++) {
var createQuery2 = '{"hash"::' + response&#46;data&#46;hash + '"}';
var createQuery = '{"hash": "' + response&#46;data&#46;hash +
'","title": "'+ response&#46;data&#46;title +
'","ext": "'+ response&#46;data&#46;ext +
'","permalink": "'+ response&#46;data&#46;permalink +
'","views": "'+ response&#46;data&#46;views +
'","datetime": "'+ response&#46;data&#46;datetime +
'","date": "'+ response&#46;data&#46;date +
'","size": "'+ response&#46;data&#46;size +
'","created": "'+ response&#46;data&#46;created +
'","width": "'+ response&#46;data[i]&#46;width +
'","animated": "'+ response&#46;data[i]&#46;animated +
'","height": "'+ response&#46;data[i]&#46;height +
'","source": "'+ response&#46;data[i]&#46;source +
'","description": "'+ response&#46;data[i]&#46;description +
'","subreddit": "'+ response&#46;data[i]&#46;subreddit +
'","author": "'+ response&#46;data[i]&#46;author +
'","score": "'+ response&#46;data[i]&#46;score +
'","ups": "'+ response&#46;data[i]&#46;ups +
'","downs": "'+ response&#46;data[i]&#46;downs +
'","points": "'+ response&#46;data[i]&#46;points +
'"}';
console&#46;log(createQuery2);
create_data&#46;execute({"data":JSON&#46;stringify(createQuery2)});
create_data&#46;execute({"data":JSON&#46;stringify(createQuery)});
}
/code/pre
Please help me with the code. createQuery2 is just for debugging.


Insert records on success event

Posted: Mon Feb 17, 2014 8:13 am
by Kateryna Grynko

Hi Ralph,

Do you want to write it to database? If yes, you should pass not the 'data' parameter but all the field you have in collection.


Insert records on success event

Posted: Mon Feb 17, 2014 8:43 am
by Ralph Ngo

Hi Katya,

Yes, I want to write into database. What if some of the fields are null? What should I input?


Insert records on success event

Posted: Mon Feb 17, 2014 8:44 am
by Ralph Ngo

Also, if I put a variable in execute script, the editor will indicate I have error.

e.g: create_data.execute({createQuery});


Insert records on success event

Posted: Mon Feb 17, 2014 9:01 am
by Ralph Ngo

One more question, do we need the data format to be the same as in the collection?


Insert records on success event

Posted: Mon Feb 17, 2014 10:39 am
by Kateryna Grynko

Ralph,

You set parameters incorrectly. Take a look at here please: https://getsatisfaction.com/apperyio/...


Insert records on success event

Posted: Mon Feb 17, 2014 11:05 am
by Ralph Ngo

Hi Katya,

I tried with following code, but failed:

precodefor (var i=0; i<response&#46;data&#46;length; i++) {
var createQuery2 = '{"hash":' + response&#46;data&#46;hash + '"}';
var createQuery = '{"hash": "' + response&#46;data&#46;hash +
'","title": "'+ response&#46;data&#46;title +
'","ext": "'+ response&#46;data&#46;ext +
'","permalink": "'+ response&#46;data&#46;permalink +
'","views": '+ response&#46;data&#46;views +
',"datetime": "'+ response&#46;data&#46;datetime +
'","date": "'+ response&#46;data&#46;date +
'","size": '+ response&#46;data&#46;size +
',"created": '+ response&#46;data&#46;created +
',"width": '+ response&#46;data[i]&#46;width +
',"animated": "'+ response&#46;data[i]&#46;animated +
'","height": '+ response&#46;data[i]&#46;height +
',"source": "'+ response&#46;data[i]&#46;source +
'","description": "'+ response&#46;data[i]&#46;description +
'","subreddit": "'+ response&#46;data[i]&#46;subreddit +
'","author": "'+ response&#46;data[i]&#46;author +
'","score": '+ response&#46;data[i]&#46;score +
',"ups": '+ response&#46;data[i]&#46;ups +
',"downs": '+ response&#46;data[i]&#46;downs +
',"points": '+ response&#46;data[i]&#46;points +
'}';
console&#46;log(createQuery);
create_data&#46;execute({
data: createQuery
});
}/code/pre

Console shows createQuery as below:
precode
{"hash": "EdV9edq","title": "Strawberries [2560x1440]","ext": "&#46;jpg","permalink": "/r/wallpapers/comments/1xshj3/strawberries_2560x1440/","views": 3160,"datetime": "2014-02-13 09:29:12","date": "2014-02-13","size": 227120,"created": 1392283752,"width": 2560,"animated": "0","height": 1440,"source": "null","description": "null","subreddit": "wallpapers","author": "TheFinnishCyborg","score": 1308,"ups": 1,"downs": 0,"points": 1} /code/pre


Insert records on success event

Posted: Mon Feb 17, 2014 7:08 pm
by Maryna Brodina

Hello!
[quote:]do we need the data format to be the same as in the collection?[/quote] yes. If there is string, you have to pass string; if there is date, you have to pass date and so on. Please pay attention on fields names, they are case sensitive.

What error did you get from service?


Insert records on success event

Posted: Tue Feb 18, 2014 12:43 am
by Ralph Ngo

Hi, I tried JSON.stringify() but still can not import data into database. There is no error shown. There are blank records in my database after I run the script. My script is (after adjusted with JSON.stringify):

precode
var response = JSON&#46;parse(localStorage&#46;getItem("json_response"));
for (var i=0; i<response&#46;data&#46;length; i++) {
var createQuery2 = '{"hash":' + response&#46;data&#46;hash + '"}';
var createQuery = '{"hash": "' + response&#46;data&#46;hash +
'","title": "'+ response&#46;data&#46;title +
'","ext": "'+ response&#46;data&#46;ext +
'","permalink": "'+ response&#46;data&#46;permalink +
'","views": '+ response&#46;data&#46;views +
',"datetime": "'+ response&#46;data&#46;datetime +
'","date": "'+ response&#46;data&#46;date +
'","size": '+ response&#46;data&#46;size +
',"created": '+ response&#46;data&#46;created +
',"width": '+ response&#46;data[i]&#46;width +
',"animated": "'+ response&#46;data[i]&#46;animated +
'","height": '+ response&#46;data[i]&#46;height +
',"source": "'+ response&#46;data[i]&#46;source +
'","description": "'+ response&#46;data[i]&#46;description +
'","subreddit": "'+ response&#46;data[i]&#46;subreddit +
'","author": "'+ response&#46;data[i]&#46;author +
'","score": '+ response&#46;data[i]&#46;score +
',"ups": '+ response&#46;data[i]&#46;ups +
',"downs": '+ response&#46;data[i]&#46;downs +
',"points": '+ response&#46;data[i]&#46;points +
'}';
console&#46;log(JSON&#46;stringify(createQuery));
create_data&#46;execute({
data: JSON&#46;stringify(createQuery)
});
}
/code/pre


Insert records on success event

Posted: Tue Feb 18, 2014 12:53 am
by Ralph Ngo

Thank you all of you. I found the solution. I should use following code instead of joining each item manually.
precodevar createQuery = response&#46;data/code/pre