I'm currently using strongloop to develop an application. Although I have already specified my id to type string, when I store a record, it came out as a string of objects.
This is my model.json file
"id": {
"type:": "string",
"id": true
},
and the result in the psql database is
{"0":"P","1":"K","2":"U","3":"W","4":"7","5":"C","6":"E","7":"B"}
Can someone point me in the right direction of why this is the case? I used a npm library to generate the string.
var shortid = require('shortid32');
var newID = shortid.generate();
Anyone knows what the problem is?
Add below configuration to id :
"id": {
"type:": "string",
"id": true,
"postgresql": {
"columnName": "id",
"dataType": "character varying",
"nullable": "NO"
}
},
Related
I have a collection named templates. It has a field 'sec_1' which is an array of objects with properties 'name' and 'des' both of type string. Now I'm using elastic search and when I try to execute a query that matches only title fields I get an error saying the sec_1 field is not nested. Upon checking the mappings here is what I get mapping type of sec_1 is text.
I checked online and saw the fix was to modify the mapping. I created a new index like:
`curl base_url/new_index_name
{
"mappings":
{"properties": {
"sec_1": {
"type":
"nested","properties": {
"name": { "type": "text" },
"des": { "type": "text" }
}
}
}
}
}
and then reindexed like this:
curl base_url/_reindex
{
"source": {
"index": "old_index"
},
"dest": {
"index": "new_index"
}
}`
First request is successful, the second one fails with this error:
{
"index": "new_index",
"type": "_doc",
"id": "be5123a4-d0e8-4d7b-a8f1-42f31d37fe55",
"cause": {
"type": "mapper_parsing_exception",
"reason": "object mapping for [sec_1] tried to parse field [null] as object, but found a concrete value"
},
"status": 400
},
I don't understand why this is happening and what I'm doing wrong. Please help. I do not know much about elastic search, I've looked for solutions online, tried chatgpt too but the same steps appear. I'm finding the reason for this error is that sec_1 is not nested type but I've checked in database it is an array of objects. What else could be wrong?
I tried creaing new index but cannot do that and without new index my search query cant function properly
I am making a db query upon hitting a POST API endpoint. The query needs to update the Json column in my networks table, which only has 3 columns (id, name, and json). I need to specifically update the coreEssentials array with another value, so I have been using the set 'json' = ? SQL query where I paste in the entire column with my changes in the specific field and it works (manually in the db). The only issues are, I need to do make a SQL call to SELECT the json column for a specific id first, (long story, but a backend application generates some data into the JSON (the coreEssentials key/object I need to update) then puts it into the data, then after I need to update).
I was doing this manually in my Postgresql GUI (DBbeaver) and my query simply looks like this:
update network set "json" = '{
"uid": "randomUid",
"etag": "randomEtag",
"name": "randomNameAgain",
"state": "PENDING",
"Type": "ABC",
"version": 1,
"dealerId": "random_uuid",
"Param": {
"AreaId": 0,
"AreaIdStr": "0.0.0.0",
"DeadInterval": 0,
"HelloInterval": 0
},
"networkData": {
"tic": "311",
"toe": "980",
"tac": "201",
"tac_id": "201",
"timeZone": null
},
"production": false,
"customerName": "random_name",
"IPPool": "0.0.0.0/32",
"customerEmail": "random#email.com",
"coreEssentials": [ ],
"deployment": "A"
}'
coreEssentials starts out as an Empty array but I need to set it to this:
[{
"version": 1,
"component": "purple",
"instanceId": "1"
},
{
"version": 1,
"component": "gray",
"instanceId": "1"
},
{
"version": 1,
"component": "blue",
"instanceId": "1"
} ]
I'm using a Node JS backend with pg-promise (Postgresql) library. Can anyone give me advice how to do this query?
I just set coreEssentials array to the data object returned from the first SQL query like #vitaly-t suggested :D
I would like to get a modified response object. For example I dont know how to get the user object without the roles.
The default response is:
{
"id": 6,
"username": "username",
"email": "user#email.com",
"provider": "local",
"confirmed": true,
"blocked": false,
"role": {
"id": 2,
"name": "Authenticated",
"description": "Default role given to authenticated user.",
"type": "authenticated"
}
}
Now I want to get the same response without the role attribute.
{
"id": 6,
"username": "username",
"email": "user#email.com",
"provider": "local",
"confirmed": true,
"blocked": false
}
Currently you cannot do this in the Rest API unless you change the UserController provided by permissions plugin, which is not recommended.
What you can do then is to use the GraphQL plugin provided by Strapi, so you can query only the fields you need on client side.
The docs about how to use GraphQL plugin are here.
For anyone still struggling with this problem:
The latest versions of strapi do support custom queries, you can pass an array containing all the names of relations you wish to populate (only relations!).
If you don't want to populate any relationships, you can keep it empty, your controller would then look something like this:
module.exports = {
UserWithoutRoles: ctx => {
return strapi.query('user').findOne({ id: ctx.params.id }, ['']);
}
}
If you do wish to populate it, it would be like this:
module.exports = {
UserWithoutRoles: ctx => {
return strapi.query('user').findOne({ id: ctx.params.id }, ['role']);
}
}
Also see:
[https://strapi.io/documentation/3.0.0-beta.x/concepts/queries.html#api-reference][1]
I'm trying to get the latest records, grouped by the field groupId, which is a String like "group_a".
I followed the accepted answer of this question, but I've got the following error message:
Fielddata is disabled on text fields by default. Set fielddata=true on [your_field_name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory.
In the Elasticsearch docs is written:
Before you enable fielddata, consider why you are using a text field for aggregations, sorting, or in a script. It usually doesn’t make sense to do so.
I'm using a text field, because groupId is a String. Does it make sense to set fielddata: true if I want to group by it?
Or are there alternatives?
Using "field": "groupId.keyword" (suggested here) didn't work for me.
Thanks in advance!
The suggest answer with .keyword is the correct one.
{
"aggs": {
"group": {
"terms": {
"field": "groupId.raw"
},
"aggs": {
"group_docs": {
"top_hits": {
"size": 1,
"sort": [
{
"timestamp (or wathever you want to sort)": {
"order": "desc"
}
}
]
}
}
}
}
}
}
with a mapping like that:
"groupId": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
First of all, i want to let you know that i am a novice with node and couchDB and i have this project where i need to add some functionality to the existing application.
So, i have javascript/node/express web application and i want to get a specific document from a remote couchDB, using cradle, and then get a list of its fields and their values. Later on i would need to display those fields/values in some html.
I don't know which fields the document has because they are dynamically added/removed by a third party.
I was able to get the document i wanted, but i don't know how to iterate through its fields. What would be the best way to do that?
Here is a simpificated sample of the document:
{
"_id": "1.1.5",
"_rev": "5-56ebac233e7f56a14a4534c6902727f7",
"1.1.5.39": {
"Project": {
"Project1": {
"files": "...",
"status": "NEW",
"id": 2
},
"Project2": {
"files": "...",
"status": "ASSIGNED",
"id": 3
}
}
}
"1.1.5.23": {
"Project": {
"Project3": {
"files": "...",
"status": "NEW",
"id": 4
},
"Project4": {
"files": "...",
"status": "NEW",
"id": 5
}
}
}
}
I would need to get the fields '1.1.5.39' and '1.1.5.23', and also the 'status' values. These fields represent some versions of a software. The problem is also fields' format: numbers and dots, so i can't just use 'Object.attribute' notation...
First part is getting the document via cradle:
// get the Connection
var connection = new(cradle.Connection)('http://living-room.couch', 5984, {
cache: true,
raw: false
});
// get the DB
var db = connection.db('yourDB');
// get the document
var docID = "1.1.5";
db.get('docID', function (err, doc) {
displayDoc(doc);
});
function displayDoc(doc) {
// Do your display handling here
}
The second part what you need is displaying arbitrary objects. Here I refer you to my answer to this question.
Good Luck.