我想知道如何将对象的对象作为动作中的输入 - 例如,
module.exports = {
friendlyName: 'Signup',
description: 'Signup a user for an account.',
inputs: {
bride: {
firstName: {
description: 'The first name of the bride',
type: 'string',
required: true
}
},
groom: {
lastName: {
description: 'The first name of the groom',
type: 'string',
required: true
}
}
}
exits: {},
fn: async function (inputs, exits) {
sails.log.debug(inputs.bride.firstName); // I would like to be able to reference input like this
return exits.success();
}
};
如何执行此操作并访问inputs.bride.firstName(而不必执行inputs.brideFirstName)?
尝试执行此操作时收到以下错误:
'Invalid input definition ("bride"). Must have `type`, or at least some more information. (If you aren\'t sure what to use, just go with `type: \'ref\'.)' ] } }
答案 0 :(得分:1)
另一种方法是使用对象对象实现自定义验证
module.exports = {
friendlyName: 'Signup',
description: 'Signup a user for an account.',
inputs: {
bride: {
description: 'The first name of the bride',
type: 'json', // {'firstName': 'luis'}
custom: function(value) {
return _.isObject(value) && _.isString(value.firstName)
}
},
groom: {
description: 'The first name of the groom',
type: 'json', // {'lastname': 'lazy'}
custom: function(value) {
return _.isObject(value) && _.isString(value.lastName)
}
}
}
exits: {},
fn: async function (inputs, exits) {
sails.log.debug(inputs.bride.firstName); // luis
return exits.success();
}
};
有关以下内容的更多信息:https://sailsjs.com/documentation/concepts/models-and-orm/validations#?custom-validation-rules
答案 1 :(得分:0)
在这种情况下,您应该使用json类型。 Sails不会检查json类型的属性。
module.exports = {
friendlyName: 'Signup',
description: 'Signup a user for an account.',
inputs: {
bride: {
description: 'The first name of the bride',
type: 'json', // {'firstName': 'luis'}
required: true
},
groom: {
description: 'The first name of the groom',
type: 'json', // {'lastname': 'lazy'}
required: true
}
}
exits: {},
fn: async function (inputs, exits) {
sails.log.debug(inputs.bride.firstName); // luis
return exits.success();
}
};
有关类型的更多信息:https://sailsjs.com/documentation/concepts/models-and-orm/attributes#?type