Select to view content in your preferred language

Uncaught TypeError: C.createPostQuery is not a function

881
5
08-09-2021 11:32 AM
RobertBossio
Emerging Contributor

I'm trying to save a layer created on client web application using JavaScript API 4.20 to the user's portal. I'm using portal.user.addItem function but get error, "Uncaught TypeError: C.createPostQuery is not a function". 

Any examples of saving a layer to user's portal using portal.user.addItem? I can successfully read user's layers from the portal, and save one back (changing some values to make it unique). Is the issue that the layer is created on the client (FeatureLayer) and then saved to the portal? Any way of doing this with the JavaScript API? 

Any info appreciated.

0 Kudos
5 Replies
RichardDuty
Emerging Contributor

Hello Robert, were you able to figure out what was wrong here? I currently have the same issue. I was able to save a new item at some point although it was not sending the geometries with it. Since then I've implemented some other features into my project but now that I've come back to this I get this error.

0 Kudos
rbossio
Emerging Contributor

Hi Richard - I've since abandoned how I was doing that. The correct approach (with help from an Esri developer) to save a layer/map from Web application is to follow these 3 steps, calling the Esri RESTful API:

1. Create Feature Service (https://developers.arcgis.com/rest/users-groups-and-items/create-service.htm)

2. Add to Definition (to feature service) - (https://developers.arcgis.com/rest/services-reference/online/add-to-definition-feature-service-.htm)

3. AddFeatures (adds points to definition) - (https://developers.arcgis.com/rest/services-reference/enterprise/add-features.htm)

Note: You have to be logged-in to an existing Esri account (ArcGIS Online or an Enterprise instance) 

Hope this helps.

RichardDuty
Emerging Contributor

Thank you for the update. I will give that a try. Have a good evening.

0 Kudos
RichardDuty
Emerging Contributor

I was finally able to get the service created through the method that you shared but I am a little lost on what the URLs should look like for the other two steps. Can you provide any insight on those?

0 Kudos
rbossio
Emerging Contributor

Hi Richard - sorry for the delay as I'm just now noticing your request. Here's example URLs. Hope this helps if you haven't already figured it out:

1. CreateFeatureLayer

var createParameters = {
"name": `some unique name`,
"hasStaticData": false,
"maxRecordCount": 1000000,
"supportedQueryFormats": "JSON",
"capabilities": "Create,Delete,Query,Update,Editing",
"description": description,
"copyrightText": "©2021 Company",
"spatialReference": {
"wkid": 102100,
"latestWkid": 102100
},
"minScale": 0,
"maxScale": 0,
"allowGeometryUpdates": true,
"units": "esriMeters",
"xssPreventionInfo": {
"xssPreventionEnabled": true,
"xssPreventionRule": "input",
"xssInputRule": "rejectInvalid"
}
};

const settings = {
"url": portal.user.userContentUrl + "/createservice",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/x-www-form-urlencoded"
},
"data": {
"createparameters": JSON.stringify(createParameters),
"f": "json",
"token": portal.credential.token,
"outputtype": "featureService",
"description": "some Description",
"tags": "tag",
"snippet": "some snippet info"
}
};

$.ajax(settings)
.done(function (response) {
try {
if (response.success) {
return resolve(response);
} else {
return reject(response);
}
} catch (error) {
return reject(error);
}
}).fail(function (xhr, status, error) {
return reject(error);
});
} catch (error) {
return reject(error);
}

2. addToDefinition

const YOUR_TOKEN = portal.credential.token;
const SERVICE_NAME = serviceName; // returned from CreateFeatureLayer
const HOST = host; // returned from CreateFeatureLayer, something like Service7
const INSTANCE = portal.id; // Something like: d9R4ThD32qsG1Wu4
let apiUrl = `https://${HOST}.arcgis.com/${INSTANCE}/arcgis/rest/admin/services/${SERVICE_NAME}/FeatureServer/addToDefinition`;

var layerDefinition = {
layers: [
{
"id": 0,
"name": "Unique Name",
"type": "Feature Layer",
"allowGeometryUpdates": true,
"description": "Feature Layer that contains relative statistcal / analytical data",
"summary": "Feature layer summary",
"copyrightText": "©2021 Company",
"defaultVisiblity": true,
"visibilityField": "visible",
"capabilities": "Query,Create,Delete,Update,Editing",
"geometryType": "esriGeometryPoint",
"editingInfo": { "lastEditDate": null },
"hasAttachments": false,
"hasStaticData": false,
"hasM": false,
"hasZ": false,
"htmlPopupType": "esriServerHTMLPopupTypeAsHTMLText",
"isDataVersioned": false,
"maxRecordCount": 100000,
"standardMaxRecordCount": 100000,
"tileMaxRecordCount": 8000,
"maxRecordCountFactor": 1,
"exceedsLimitFactor": 1,
"globalIdField": "globalid",
"objectIdField": "fid",
"uniqueIdField": {
"name": "fid",
"isSystemMaintained": true
},
"hasStaticData": false,
"ownershipBasedAccessControlForFeatures": true,
"allowTrueCurvesUpdates": true,
"onlyAllowTrueCurveUpdatesByTrueCurveClients": true,
"supportedQueryFormats": "JSON, geoJSON",
"supportsRollbackOnFailureParameter": true,
"supportsAppend": true,
"supportsCalculate": true,
"supportsTruncate": true,
"supportsAttachmentsResizing": true,
"supportsStatistics": true,
"supportsValidateSql": true,
"supportsCoordinatesQuantization": true,
"supportsApplyEditsWithGlobalIds": false,
"supportsAttachmentsByUploadId": true,
"useStandardizedQueries": true,
"supportsAdvancedQueries": true,
"advancedQueryCapabilities": {
"supportsPagination": true,
"supportsPaginationOnAggregatedQueries": true,
"supportsQueryRelatedPagination": true,
"supportsQueryWithDistance": true,
"supportsReturningQueryExtent": true,
"supportsStatistics": true,
"supportsOrderBy": true,
"supportsDistinct": true,
"supportsQueryWithResultType": true,
"supportsSqlExpression": true,
"supportsAdvancedQueryRelated": true,
"supportsCountDistinct": true,
"supportsLod": true,
"supportsReturningGeometryCentroid": false,
"supportsQueryWithDatumTransformation": true,
"supportsHavingClause": true,
"supportsOutFieldSQLExpression": true
},
"typeIdField": null,
"indexes": [],
"types": [],
"relationships": [],
"drawingInfo": {
"renderer": layer.renderer,
"transparency": 0,
},
"extent": {
"xmin": extent.xmin,
"ymin": extent.ymin,
"xmax": extent.xmax,
"ymax": extent.ymax,
"spatialReference": {
"wkid": 102100,
"latestWkid": 102100
}
},
"fields": [
{
"name": "fieldname",
"alias": "fieldnamealias",
"type": "esriFieldTypeString",
"sqlType": "sqlTypeNVarchar",
"length": 256,
"nullable": true,
"editable": true,
"domain": null,
"defaultValue": null
}, .... more fields ....,
],
"templates": [
{
"name": "New Feature",
"description": "New Feature Layer",
"drawingTool": "esriFeatureEditToolPoint",
"prototype": {
"attributes": {
"fieldname": null,
...more field names...
}
}
}
]
}
],
tables: []
};

const settings = {
"url": apiUrl,
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/x-www-form-urlencoded"
},
"data": {
"addToDefinition": JSON.stringify(layerDefinition),
"f": "json",
"token": YOUR_TOKEN
}
};

$.ajax(settings)
.done(function (response) {
try {
if (response.success) {
return resolve(response);
} else {
return reject(response);
}
} catch (error) {
return reject(error);
}

}).fail(function (xhr, status, error) {
return reject(error);
});
});
} catch (error) {
return reject(error);
}

3. Add Features to Layer

let serviceUrl = featureServiceURL; //from CreateFeatureLayer response
let featureSet = features[]; //array of features to add to layer
const settings = {
"url": serviceUrl + "/0/addFeatures",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/x-www-form-urlencoded"
},
"data": {
"features": JSON.stringify(featureSet),
"f": "json",
"token": token
}
};

$.ajax(settings)
.done(function (response) {
//need to handle for an error coming back as string
let errorMessage;
try {
let responseObj = $.parseJSON(response);
if (responseObj.error)
errorMessage = responseObj.error.message;
} catch (error) {
//display error
}
})
.fail(function (xhr, status, error) {
//display error
});

0 Kudos