我试图在休息查询中使用默认模板:
/company/somecompanyid?template=default
但我仍然从我的mongodb获取所有数据,包括不在模板中的相关表的字段和集合。
这也被定义为模型中的defaultTemplate,但它似乎不会以任何方式影响结果。
有人可以解释我做错了什么以及如何应用模板?
如果我想在回复中仅包含对象Id而不是整个相关对象,我该如何在模板中指定它?
company.js:
'use strict';
/**
* Module dependencies
*/
// Node.js core.
const path = require('path');
// Public node modules.
const _ = require('lodash');
const anchor = require('anchor');
// Local dependencies.
const WLValidationError = require('../../../node_modules/waterline/lib/waterline/error/WLValidationError');
// Settings for the Company model.
const settings = require('./Company.settings.json');
/**
* Export the Company model
*/
module.exports = {
/**
* Basic settings
*/
// The identity to use.
identity: settings.identity,
// The connection to use.
connection: settings.connection,
// Do you want to respect schema?
schema: settings.schema,
// Limit for a get request on the list.
limit: settings.limit,
// Merge simple attributes from settings with those ones.
attributes: _.merge(settings.attributes, {
}),
// Do you automatically want to have time data?
autoCreatedAt: settings.autoCreatedAt,
autoUpdatedAt: settings.autoUpdatedAt,
/**
* Lifecycle callbacks on validate
*/
// Before validating value
beforeValidate: function (values, next) {
// WARNING: Don't remove this part of code if you don't know what you are doing
const api = path.basename(__filename, '.js').toLowerCase();
if (strapi.api.hasOwnProperty(api) && _.size(strapi.api[api].templates)) {
const template = strapi.api[api].templates.hasOwnProperty(values.template) ? values.template : strapi.models[api].defaultTemplate;
const templateAttributes = _.merge(_.pick(strapi.models[api].attributes, 'lang'), strapi.api[api].templates[template].attributes);
// Handle Waterline double validate
if (_.size(_.keys(values)) === 1 && !_.includes(_.keys(templateAttributes), _.keys(values)[0])) {
next();
} else {
const errors = {};
// Set template with correct value
values.template = template;
values.lang = _.includes(strapi.config.i18n.locales, values.lang) ? values.lang : strapi.config.i18n.defaultLocale;
_.forEach(templateAttributes, function (rules, key) {
if (values.hasOwnProperty(key)) {
// Check rules
const test = anchor(values[key]).to(rules);
if (test) {
errors[key] = test;
}
} else if (rules.required) {
errors[key] = [{
rule: 'required',
message: 'Missing attributes ' + key
}];
}
});
// Go next step or not
_.isEmpty(errors) ? next() : next(new WLValidationError({
invalidAttributes: errors,
model: api
}));
}
} else if (strapi.api.hasOwnProperty(api) && !_.size(strapi.api[api].templates)) {
next();
} else {
next(new Error('Unknow API or no template detected'));
}
}
/**
* Lifecycle callbacks on create
*/
// Before creating a value.
// beforeCreate: function (values, next) {
// next();
// },
// After creating a value.
// afterCreate: function (newlyInsertedRecord, next) {
// next();
// },
/**
* Lifecycle callbacks on update
*/
// Before updating a value.
// beforeUpdate: function (valuesToUpdate, next) {
// next();
// },
// After updating a value.
// afterUpdate: function (updatedRecord, next) {
// next();
// },
/**
* Lifecycle callbacks on destroy
*/
// Before updating a value.
// beforeDestroy: function (criteria, next) {
// next();
// },
// After updating a value.
// afterDestroy: function (destroyedRecords, next) {
// next();
// }
};
模板是公司属性的子集(CompanyDefault.template.json):
{
"default": {
"attributes": {
"name": {
"required": true
},
"address": {},
"phone": {},
"email": {}
},
"displayedAttribute": "name"
}
}
答案 0 :(得分:0)
您能告诉我们您的默认模板文件吗?然后,要使用模板系统,您必须从Studio或CLI生成模型。模板逻辑位于" /api/company/models/Company.js"在你的情况下档案。向我们展示这个文件也会很有趣。
您正在使用正确的方式应用模板。你的网址好看!但是,目前不可能仅包括对象ID而不是整个相关对象。为此,我建议您使用GraphQL或RAW查询...
在V2中,我们将支持Strapi中的JSON API规范,这肯定适合您的问题!
PS:我是Strapi的作者之一。