gpt4 book ai didi

javascript - 环回推送数组

转载 作者:行者123 更新时间:2023-12-03 07:45:32 24 4
gpt4 key购买 nike

我将如何制作一个自定义remoteMethod来更新/推送,不覆盖,一个数组的属性。因此,基本上将数据推送到模型的数组属性中。

除了 .add() 辅助方法之外,我在文档中找不到任何明确的示例,但这需要 embedsOne 或其他某种关系.

但是,如果我的整个应用程序中有一个单个 模型,并且只想推送一些数据到id,该怎么办。因此最终得到如下端点:

POST/Thing/{id}/pushData

POST 的正文将是:

  {
id: "foo",
data: "bar"
}

(或者最好没有 id,并且有 id autoInserted,因为它是一个数组,而且我不需要 id每个项目的data 部分应该可以使用过滤器/where 进行搜索

到目前为止我已经:

  Thing.remoteMethod (
'pushData',
{
isStatic: false,
http: {path: '/pushData', verb: 'post'},
accepts: [
{ arg: 'data', type: 'array', http: { source: 'body' } }
],
returns: {arg: 'put', type: 'string'},
description: 'push some Data'
}
);

Thing.prototype.pushData = function(data, cb) {
data.forEach(function (result) {
// ??
});
cb(null, data)
};

据我所知,默认端点仅允许添加单个实例,但我想批量更新。

最佳答案

您已将方法设为非静态,这很好。

现在,如果您的数组属性名为 MyArray,我会尝试以下操作:

  Thing.remoteMethod (
'pushData',
{
isStatic: false,
http: {path: '/pushData', verb: 'post'},
accepts: [
{ arg: 'data', type: 'array', http: { source: 'body' } }
],
returns: {arg: 'put', type: 'string'},
description: 'push some Data'
}
);

Thing.prototype.pushData = function(data, cb) {
thingInstance = this;
data.forEach(function (result) {
thingInstance.MyArray.push(result.data);
});

cb(null, data)
};

由于您的远程方法是非静态的,因此您应该能够使用 this 访问实例。我怀疑您是否可以通过编写 this.someProperty 来直接访问属性,请尝试一下,如果不起作用,请告诉我。

然后要批量创建,只需向远程发出标准 POST 请求

POST /Thing/{id}/pushData

像这样编写你的 JSON

{
{
data: "bar"
},
{
data: "foo"
},
//etc
}

这应该向数组属性添加两个元素。

请告诉我这是否有帮助

关于javascript - 环回推送数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35223393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com