3

I want to publish a new layer in Geoserver. The layer seems unpublished and I get 'Undefined' error.

const publish = function () {

return new Promise((resolve, reject) => {
    const layer = layerName
    const workspace = workspaceName
    const datastore = datastoreName

    let body = `<featureType><name>${layer}</name>
            <nativeBoundingBox>
                <minx>-180</minx>
                <maxx>180</maxx>
                <miny>-90</miny>
                <maxy>90</maxy>
                <crs>EPSG:4326</crs>
            </nativeBoundingBox>
            <latLonBoundingBox>
                <minx>-180</minx>
                <maxx>180</maxx>
                <miny>-90</miny>
                <maxy>90</maxy>
                <crs>EPSG:4326</crs>
            </latLonBoundingBox>
        </featureType>`

    console.log('body length is ', body.length);


    let options = {
        url: `http://IP:port/geoserver/rest/workspaces/datastores/featuretypes.xml`,
        method: 'POST',
        headers: {
            "Content-Type": "text.xml",
            "Content-Length": body.length
        },
        auth: {
            'user': 'user',
            'pass': 'pass'
        },

    }
    request(options, (err, resp, body) => {
        if (!err && !body) {
            return resolve()

        } else if (!err) {
            let message
            if (resp.statusCode == 400) message = "layer not found"
            else message = body

            return reject({
                status: 404,
                errors: [{
                    messages: [message]
                }]
            })

        } else {
            return reject({
                status: 500,
                errors: [{
                    messages: ["ERRO: connection with geoserver"]
                }]
            })
        }
    })
 })
}

publish().then(result => console.log(result))
5
  • where do you get an undefined error?
    – Ian Turton
    Commented Jun 25, 2019 at 8:02
  • From ' return resolve' I guess.
    – Angels
    Commented Jun 25, 2019 at 8:05
  • Well, he invokes resolve without any arguments so even a correct request will log undefined.
    – dmh126
    Commented Jun 25, 2019 at 8:06
  • 'if (!err && !body) { return resolve({ status: 200, message: 'Layer is published' }) }' still the layer is unpublished.
    – Angels
    Commented Jun 25, 2019 at 8:13
  • Your code worked well. But I still where is my fault
    – Angels
    Commented Jun 25, 2019 at 8:27

1 Answer 1

4

The url seems to be invalid.

I use: ${geoserver.url}/geoserver/rest/workspaces/${geoserver.namespace}/datastores/${geoserver.store}/featuretypes

And it's fine. Ex. localhost:8080/geoserver/rest/workspaces/testworkspace/datastores/teststore/featuretypes

And the function I use is:

export const publish = (name) => {

    const geoserver = config.geoserver;

    return new Promise( (resolve, reject) => {

        axios({
            method: 'POST',
            url: `${geoserver.url}/geoserver/rest/workspaces/${geoserver.namespace}/datastores/${geoserver.store}/featuretypes`,
            auth: {
                username: geoserver.user,
                password: geoserver.password
            },
            headers: {
                'Content-Type': 'text/xml'
            },
            data: `<featureType><name>${name}</name></featureType>`

        })
        .then( response => {
            resolve({
                ok: 1,
                message: `Layer has been published.`
            })
        })
        .catch( err => {
            reject({
                ok: 0,
                message: err.response.data,
            })
        })

    })
}
1
  • Let me try yours. But my url is fine like this: http://IP:port/geoserver/rest/workspaces/<workspaceName>/datastores/<datastoreName>/featuretypes.xml
    – Angels
    Commented Jun 25, 2019 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.