Ralph Ngo
Posts: 0
Joined: Fri Dec 27, 2013 1:07 am

Insert records on success event

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.

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Insert records on success event

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.

Ralph Ngo
Posts: 0
Joined: Fri Dec 27, 2013 1:07 am

Insert records on success event

Hi Katya,

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

Ralph Ngo
Posts: 0
Joined: Fri Dec 27, 2013 1:07 am

Insert records on success event

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

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

Ralph Ngo
Posts: 0
Joined: Fri Dec 27, 2013 1:07 am

Insert records on success event

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

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Insert records on success event

Ralph,

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

Ralph Ngo
Posts: 0
Joined: Fri Dec 27, 2013 1:07 am

Insert records on success event

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

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Insert records on success event

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?

Ralph Ngo
Posts: 0
Joined: Fri Dec 27, 2013 1:07 am

Insert records on success event

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

Ralph Ngo
Posts: 0
Joined: Fri Dec 27, 2013 1:07 am

Insert records on success event

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

Return to “Issues”