更新时间:2022-11-27 19:15:44
我认为原因是因为您没有在 Foxx 的结束点指定预期作为 .post 一部分的正文.
I think the reason is because you haven't specified in the end point in Foxx that there is a body expected as part of the .post.
我花了一段时间才找到定义 Foxx 微服务的方法,在确定模式之前,我阅读了大量 ArangoDB 示例代码.
It took me a while to work out a way of defining Foxx MicroServices, and I read through a number of ArangoDB example code before I settled on a pattern.
为了帮助您入门,我提供了如何以可扩展的方式快速模拟 Foxx 微服务代码,从而允许您将路由与模型分开.
To help you get started, I've provided how I would quickly mock up the Foxx MicroService code in a way that is extensible, allowing you to separate your Routes from your Models.
使用这些作为例子来让你的例子工作.
Use these as examples to get your example working.
我已经假设有两个文档集合,Drop"和Bot",其中一个边缘集合将它们连接起来,称为VisitedBy".
I've made assumptions that there are two document collections, 'Drop' and 'Bot' with an edge collection that joins them called 'VisitedBy'.
所有这些文件都存储在您的 Foxx 微服务上:
All these files are stored on your Foxx MicroService:
main.js
'use strict';
module.context.use('/v1/visitedBy', require('./routes/visitedBy'), 'visitedBy');
routes/visitedBy.js
'use strict';
const request = require('@arangodb/request');
const joi = require('joi');
const createRouter = require('@arangodb/foxx/router');
const VisitedBy = require('../models/visitedBy');
const visitedDataSchema = joi.object().required().description('Data that tracks a visited event');
const router = createRouter();
module.exports = router;
/*********************************************
* saveVisitedBy
* Path Params:
* none
* Query Params:
* none
* Body Params:
* body (required) The data that is used to record when something is visited
*/
router.post('/', function (req, res) {
const visitedData = req.body;
const savedData = VisitedBy.saveVisitedByData(VisitedBy.fromClient(visitedData));
if (savedData) {
res.status(200).send(VisitedBy.forClient(savedData));
} else {
res.status(500).send('Data not saved, internal error');
}
}, 'saveVisitedBy')
.body(visitedDataSchema, 'visited data')
.response(VisitedBy.savedDataSchema, 'The response after the data is saved')
.summary('Save visited data')
.description('Save visited data');
models/visitedBy.js
'use strict';
const _ = require('lodash');
const joi = require('joi');
const db = require('@arangodb').db;
const visitedByEdgeCollection = 'VisitedBy';
/*
Schema for a response after saving visitedBy data
*/
const savedDataScema = {
id: joi.string(),
data: joi.object(),
_from: joi.string(),
_to: joi.string()
};
module.exports = {
savedDataSchema: savedDataScema,
forClient(obj) {
// Implement outgoing transformations here
// Remove keys on the base object that do not need to go through to the client
if (obj) {
obj = _.omit(obj, ['_id', '_rev', '_oldRev', '_key']);
}
return obj;
},
fromClient(obj) {
// Implement incoming transformations here
return obj;
},
saveVisitedByData(visitedData) {
const q = db._createStatement({
"query": `
INSERT {
_from: @from,
_to: @to,
data: @data,
date: DATE_NOW()
} IN @@col
RETURN MERGE ({ id: NEW._id }, NEW)
`
});
q.bind('@col', visitedByEdgeCollection);
q.bind('from', visitedData.from);
q.bind('to', visitedData.to);
q.bind('data', visitedData.data);
const res = q.execute().toArray();
return res[0];
}
};
您的服务在 Swagger 界面中应如下所示:
Your service should look like this in the Swagger interface:
习惯 joi 需要一点时间,但是一旦您获得一些好的工作示例,您就可以为传入和传出数据定义出色的数据定义.
It takes a bit getting used to joi, but once you get some good working examples you can define great data definitions for incoming and outgoing data.
我希望这会有所帮助,我很难得到一个基本的微服务代码模型来清楚地说明事情是如何运作的,我相信这个例子可以做很多事情,但它应该是一个很好的起点.
I hope this helps, it was difficult for me getting a basic MicroService code model that made it clear how things operated, I'm sure a lot can be done for this example but it should be a good starting spot.