如何从服务器脚本创建新记录

How to create new records from the server script

根据 documentation,新记录可以像这样插入到数据源中:

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

但是,当我在我的服务器脚本中尝试做一些简单的事情时:

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

...我在 return 中得到这个:

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

所以看起来 newRecord 根本不是一个函数。为什么?以及如何从服务器脚本访问数据源?

how do you access datasources from a server script?

您提供的link指向服务器端API,但服务器上没有数据源(数据源仅存在于客户端)。当然,您可以在服务器上创建记录,但要查看它们,您需要在客户端重新加载数据源。

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

最明显的答案是您正试图在客户端调用服务器端 API(确保您在创建时选择了正确的脚本类型)...

如果你真的想在客户端创建新记录,那么你需要使用数据源的创建模式:

// 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
});