在环回中插入数据数组
insert array of data in loopback
我正在使用环回并且不知道如何使用 sing api 插入数据数组,例如我有类似
的数据
view: [ true, false, false ],
edit: [ false, true, false ],
update: [ false, false, true ],
product: [ 1, 2, 3]
现在我想在 table
中插入这样的数据
product | view | edit |update
----------------------------------
1 |true |false |false
2 |false |false |true
3 |false |false |true
我知道我可以使用 for 循环,但不知道在哪里使用,即这是否可以在远程挂钩方法或远程方法中使用,有人可以建议我如何做吗
http://apidocs.loopback.io/loopback/#persistedmodel-create
PersistedModel.create([data], callback) Create new instance of Model,
and save to database.
Arguments Name Type Description [data] Object or Array.
Optional data argument. Can be either a single model instance or an
array of instances.
Create 可以采用实例数组。 POST
在请求正文中包含一个数组,它将全部插入。
编辑
在你的情况下,我认为你必须创建一个远程方法。特定于模型的验证将在 before save
之后触发,但默认的 POST 方法验证将不允许您提交数组数组。这是一些示例代码,将 assembled
替换为您从数组数组
创建对象数组的方式
MyModel.assembleAndInsert = async (data, cb) => {
// Assemble the data
let assembled = [{name: 'iecream'}];
debugger;
let result = await MyModel.create(assembled);
cb(null, result);
};
MyModel.remoteMethod('assembleAndInsert', {
http: {
path: '/assembleAndInsert',
verb: 'post',
status: 200,
errorStatus: 400,
},
accepts: [{ arg: 'data', type: 'array', http: { source: 'body' } }],
returns: {
arg: 'created',
type: 'Array',
},
});
我正在使用环回并且不知道如何使用 sing api 插入数据数组,例如我有类似
的数据 view: [ true, false, false ],
edit: [ false, true, false ],
update: [ false, false, true ],
product: [ 1, 2, 3]
现在我想在 table
中插入这样的数据 product | view | edit |update
----------------------------------
1 |true |false |false
2 |false |false |true
3 |false |false |true
我知道我可以使用 for 循环,但不知道在哪里使用,即这是否可以在远程挂钩方法或远程方法中使用,有人可以建议我如何做吗
http://apidocs.loopback.io/loopback/#persistedmodel-create
PersistedModel.create([data], callback) Create new instance of Model, and save to database.
Arguments Name Type Description [data] Object or Array.
Optional data argument. Can be either a single model instance or an array of instances.
Create 可以采用实例数组。 POST
在请求正文中包含一个数组,它将全部插入。
编辑
在你的情况下,我认为你必须创建一个远程方法。特定于模型的验证将在 before save
之后触发,但默认的 POST 方法验证将不允许您提交数组数组。这是一些示例代码,将 assembled
替换为您从数组数组
MyModel.assembleAndInsert = async (data, cb) => {
// Assemble the data
let assembled = [{name: 'iecream'}];
debugger;
let result = await MyModel.create(assembled);
cb(null, result);
};
MyModel.remoteMethod('assembleAndInsert', {
http: {
path: '/assembleAndInsert',
verb: 'post',
status: 200,
errorStatus: 400,
},
accepts: [{ arg: 'data', type: 'array', http: { source: 'body' } }],
returns: {
arg: 'created',
type: 'Array',
},
});