2

According to the documentation, new records can be inserted into a datasource like this:

var record = app.models.Person.newRecord();
app.saveRecords([record]);

However, when in my server script I'm trying to do something as simple as:

var item = app.models.ScheduledTournaments.newRecord()

...I'm getting this in return:

TypeError: [object Object] is not a function, it is object. at SharkScope:125 at scheduledTournaments (SharkScope:122)

So it appears that newRecord is not a function at all. Why? And how do you access datasources from a server script?

Septagram
  • 9,425
  • 13
  • 50
  • 81

1 Answers1

2

how do you access datasources from a server script?

The link you provided points to server-side API, but there are no datasources on server(datasources exist only on client side). Of course, you can create records on server, but to see them you need to reload datasource on client side.

So it appears that newRecord is not a function at all.

The most obvious answer will be that you are trying to call Sever Side API on the client(make sure, you selected right script type when created)...

If you actually want to create new record on the client side, then you need to go with datasource's create mode:

// Create a new record for datasource in auto-save mode
var create = app.datasources.MyDatasource.modes.create;

create.item.Field1 = 'a';
create.item.Field2 = 'b';
...
create.createItem(function(record) {
  // do stuff
});
Pavel Shkleinik
  • 6,298
  • 2
  • 24
  • 36
  • Sorry for the confusion. When I first posted the question, I was indeed trying to work with `app.datasources` on the server side, hence "datasource" in the question title. Later I found the actual server side API, and tried to use it, but failed, editing the question to reflect that. I am **not** trying to use server side API on the client. I'm getting this error in a server script and need to create these records on the server (at least because there are lots of them and the data is being fetched/processed by the server). – Septagram Aug 16 '17 at 21:47
  • As a response to the user clicking a certain button (for now; later this will be scheduled to be performed regularly), a serverside script is called. That serverside script makes a restful API call somewhere else, and the received data is being processed and is supposed to be inserted into the database. Only then does the serverside script return. – Septagram Aug 16 '17 at 22:04
  • So you are using google.script.run.MyServerFunction to call server script, right? (https://developers.google.com/apps-script/guides/html/reference/run, https://developers.google.com/appmaker/scripting/server#calling_a_server_script) – Pavel Shkleinik Aug 16 '17 at 23:04
  • 1. What is your model type (Drive Table, CloudSQL...)? 2. Can you see autocomplete for the 'newRecord' function when you type code? – Pavel Shkleinik Aug 16 '17 at 23:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152103/discussion-between-pavel-shkleinik-and-septagram). – Pavel Shkleinik Aug 16 '17 at 23:20
  • Sorry, I couldn't make it there. Before your last comment I left to get some sleep. Thank you for the help, the error was mine after all (writing without semicolons has occasional downsides, after all). I'm marking this answer as accepted, because it does list a few possible causes of the problem. – Septagram Aug 18 '17 at 23:22