1

我在 ML8 中写了一个小的 rest 扩展,它基本上存储上传到 ml 内容数据库的文件。当我这样做时,我得到以下异常

<error-response xmlns="http://marklogic.com/xdmp/error">
<status-code>500</status-code>
<status>Internal Server Error</status>
<message-code>INTERNAL ERROR</message-code>
<message>XDMP-UPDATEFUNCTIONFROMQUERY: xdmp:function(fn:QName("http://marklogic.com/rest-api/resource/repoLoad","post"), "/marklogic.rest.resource/repoLoad/assets/resource.xqy")($context, $service-params, $input) -- Cannot apply an update function from a query . See the MarkLogic server error log for further detail.</message>
</error-response>

但我确实设置了声明选项 xdmp:update "true"; 以下是代码..

module namespace repoLoad = "http://marklogic.com/rest-api/resource/repoLoad";

declare namespace rapi = "http://marklogic.com/rest-api";

declare default function namespace  "http://www.w3.org/2005/xpath-functions";
declare option xdmp:mapping "false";
declare option xdmp:update "true";

(: Function responding to GET method - must use local name 'get':)
declare function repoLoad:get($context as map:map, $params  as map:map) as document-node()*
{
    repoLoad:notSuportedMsg($context)
};

(: Function responding to PUT method - must use local name 'put'. :)
declare function repoLoad:put($context as map:map, $params  as map:map, $input as document-node()*) as document-node()?
{
    repoLoad:notSuportedMsg($context)
};

(: Func responding to DELETE method - must use local name 'delete'. :)
declare function repoLoad:delete($context as map:map,$params  as map:map) as document-node()?
{
    repoLoad:notSuportedMsg($context)
};

(: Function responding to POST method - must use local name 'post'. :)
declare function repoLoad:post($context as map:map, $params  as map:map,$input as document-node()*) as document-node()*
{
    let $filename := xdmp:get-request-field-filename("upload")
    let $contentType := xdmp:get-request-field-content-type("upload")
    let $log := xdmp:log("File Name : " || $filename)
    let $log := xdmp:log("Content Type : " || $contentType)


    let $uri := "/documents/"||$filename
    let $_ := xdmp:document-insert($uri, xdmp:get-request-field("upload"),(xdmp:default-permissions()), ("raw"))

    let $output := json:object()
    let $_ := (
        map:put($output, "Name", $filename),
        map:put($output, "Mime-Type", $contentType)
    )
    return document {xdmp:to-json($output)}

};



declare private function repoLoad:notSuportedMsg($context as map:map) as document-node()*
{
    let $_ := map:put($context, "output-status", (501, "Not Supported HTTP method"))
    let $output := json:object()
    let $errorResponse := json:object()
    let $_ := (
        map:put($errorResponse, "statusCode", 501),
        map:put($errorResponse, "message", "Not Supported HTTP method")
    )
    let $dummpy := map:put($output, "errorResponse", $errorResponse )
    return document {xdmp:to-json($output)}
}; 

以下是日志文件中的错误跟踪

2016-02-15 17:29:32.085 Notice: content-repo: XDMP-UPDATEFUNCTIONFROMQUERY: xdmp:function(fn:QName("http://marklogic.com/rest-api/resource/repoLoad","post"), "/marklogic.rest.resource/repoLoad/assets/resource.xqy")($context, $service-params, $input) -- Cannot apply an update function from a query
2016-02-15 17:29:32.085 Notice: content-repo: in /MarkLogic/rest-api/lib/extensions-util.xqy, at 877:20,
2016-02-15 17:29:32.085 Notice: content-repo: in extut:call-service("repoLoad", "POST", xdmp:function(fn:QName("http://marklogic.com/rest-api/resource/repoLoad","post"), "/marklogic.rest.resource/repoLoad/assets/resource.xqy"), map:map(<map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"><map:entry key="accept-types"><map:value xsi:type="xs:string">te...</map:map>), map:map(), document{binary{""}}) [1.0-ml]
2016-02-15 17:29:32.085 Notice: content-repo:   $extension-name = "repoLoad"
2016-02-15 17:29:32.085 Notice: content-repo:   $method = "POST"
2016-02-15 17:29:32.085 Notice: content-repo:   $service = xdmp:function(fn:QName("http://marklogic.com/rest-api/resource/repoLoad","post"), "/marklogic.rest.resource/repoLoad/assets/resource.xqy")
2016-02-15 17:29:32.085 Notice: content-repo:   $context = map:map(<map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"><map:entry key="accept-types"><map:value xsi:type="xs:string">te...</map:map>)
2016-02-15 17:29:32.085 Notice: content-repo:   $service-params = map:map()
2016-02-15 17:29:32.085 Notice: content-repo:   $input = document{binary{""}}
2016-02-15 17:29:32.085 Notice: content-repo: in /MarkLogic/rest-api/lib/extensions-util.xqy, at 792:14,
2016-02-15 17:29:32.085 Notice: content-repo: in extut:invoke-service("repoLoad", "POST", "query", xdmp:function(fn:QName("http://marklogic.com/rest-api/resource/repoLoad","post"), "/marklogic.rest.resource/repoLoad/assets/resource.xqy"), map:map(<map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"><map:entry key="accept-types"><map:value xsi:type="xs:string">te...</map:map>), map:map(), document{binary{""}}, fn:false()) [1.0-ml]
2016-02-15 17:29:32.085 Notice: content-repo:   $extension-name = "repoLoad"
2016-02-15 17:29:32.085 Notice: content-repo:   $method = "POST"
2016-02-15 17:29:32.085 Notice: content-repo:   $default-txn-mode = "query"
2016-02-15 17:29:32.085 Notice: content-repo:   $service = xdmp:function(fn:QName("http://marklogic.com/rest-api/resource/repoLoad","post"), "/marklogic.rest.resource/repoLoad/assets/resource.xqy")
2016-02-15 17:29:32.085 Notice: content-repo:   $context = map:map(<map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"><map:entry key="accept-types"><map:value xsi:type="xs:string">te...</map:map>)
2016-02-15 17:29:32.085 Notice: content-repo:   $service-params = map:map()
2016-02-15 17:29:32.085 Notice: content-repo:   $input = document{binary{""}}
2016-02-15 17:29:32.085 Notice: content-repo:   $in-txn = fn:false()
2016-02-15 17:29:32.085 Notice: content-repo:   $txn-curr = "query"
2016-02-15 17:29:32.085 Notice: content-repo:   $annotation = ()
2016-02-15 17:29:32.085 Notice: content-repo:   $txn-mode = "query"
2016-02-15 17:29:32.085 Notice: content-repo: in /MarkLogic/rest-api/models/resource-model-query.xqy, at 254:8,
2016-02-15 17:29:32.085 Notice: content-repo: in rsrcmodqry:resource-post("repoLoad", map:map(<map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"><map:entry key="accept-types"><map:value xsi:type="xs:string">te...</map:map>), map:map(), document{binary{""}}, fn:false(), local:rsrcmod-callback#6) [1.0-ml]
2016-02-15 17:29:32.085 Notice: content-repo:   $resource-name = "repoLoad"
2016-02-15 17:29:32.085 Notice: content-repo:   $context = map:map(<map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"><map:entry key="accept-types"><map:value xsi:type="xs:string">te...</map:map>)
2016-02-15 17:29:32.085 Notice: content-repo:   $resource-params = map:map()
2016-02-15 17:29:32.085 Notice: content-repo:   $input = document{binary{""}}
2016-02-15 17:29:32.085 Notice: content-repo:   $in-txn = fn:false()
2016-02-15 17:29:32.085 Notice: content-repo:   $responder = local:rsrcmod-callback#6
2016-02-15 17:29:32.085 Notice: content-repo:   $service = xdmp:function(fn:QName("http://marklogic.com/rest-api/resource/repoLoad","post"), "/marklogic.rest.resource/repoLoad/assets/resource.xqy")
2016-02-15 17:29:32.085 Notice: content-repo: in /MarkLogic/rest-api/models/resource-model-query.xqy, at 219:4,
2016-02-15 17:29:32.085 Notice: content-repo: in rsrcmodqry:exec-post(map:map(<map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"><map:entry key="content-type"><map:value xsi:type="xs:string">mu...</map:map>), map:map(<map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"><map:entry key="upload"><map:value/></map:entry></map:map>...XDMP-CHILDNODEKIND: element nodes cannot have binary node children...), document{binary{""}}, local:rsrcmod-callback#6) [1.0-ml]
2016-02-15 17:29:32.085 Notice: content-repo:   $headers = map:map(<map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"><map:entry key="content-type"><map:value xsi:type="xs:string">mu...</map:map>)
2016-02-15 17:29:32.085 Notice: content-repo:   $endpoint-params = map:map(<map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"><map:entry key="upload"><map:value/></map:entry></map:map>...XDMP-CHILDNODEKIND: element nodes cannot have binary node children...)
2016-02-15 17:29:32.085 Notice: content-repo:   $input = document{binary{""}}
2016-02-15 17:29:32.085 Notice: content-repo:   $responder = local:rsrcmod-callback#6
2016-02-15 17:29:32.085 Notice: content-repo: in /MarkLogic/rest-api/endpoints/resource-service-query.xqy, at 65:8 [1.0-ml]
2016-02-15 17:29:32.085 Notice: content-repo:   $params = map:map(<map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"><map:entry key="upload"><map:value/></map:entry></map:map>...XDMP-CHILDNODEKIND: element nodes cannot have binary node children...)
2016-02-15 17:29:32.085 Notice: content-repo:   $headers = map:map(<map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"><map:entry key="content-type"><map:value xsi:type="xs:string">mu...</map:map>)
2016-02-15 17:29:32.085 Notice: content-repo:   $method = "POST"
2016-02-15 17:29:32.085 Notice: content-repo:   $body = document{binary{""}}
2016-02-15 17:29:32.216 Info: content-repo: Status 500: XDMP-UPDATEFUNCTIONFROMQUERY: xdmp:function(fn:QName("http://marklogic.com/rest-api/resource/repoLoad","post"), "/marklogic.rest.resource/repoLoad/assets/resource.xqy")($context, $service-params, $input) -- Cannot apply an update function from a query
4

1 回答 1

5

MarkLogic REST 扩展默认以特定的更新模式运行,具体取决于请求方法和单/多语句。从 MarkLogic 8 开始,POST 方法单个语句的默认更新模式已更改为query. 向您的方法添加注释,repoLoad:post如下所示:

declare %rapi:transaction-mode("update") function repoLoad:post(...)

另请参阅REST 应用程序开发人员指南

于 2016-02-15T19:23:54.430 回答