Path with '/' in json-server db.json - javascript

i use server-json to have a fake API, i have the path "playbook/active" in data.json
"playbook/active": [{
"description": "This playbook will install haproxy",
"name": "Testing playbook 3",
"tag": [
"loadbalancer",
"charge"
],
"path": "/etc/ansible/haproxy.yml",
"type": "action",
"id": "4bb107be-9efe-11e9-b3e5-bc5ff4901aa5"
},
{
"path": "google.com",
"description": "This is the playbook before execution",
"tag": [
"webserver",
"tomcat"
],
"id": "faa746b4-9cb7-11e9-9b94-bc5ff4901aa5",
"name": "mysql"
}
]
but i have this error
Error: Oops, found / character in database property 'playbook/active'.
I change to "playbook/active" but same error

Providing a complete answer (showcase example)
Configuring db.json and routes.json can do the trick for you:
db.json
{
"playbookActive": [
{
"id": 1,
"name": "Active Playbook 1",
"description": "Description 1"
},
{
"id": 2,
"name": "Active Playbook 2",
"description": "Description 2"
}
]
}
routes.json
{
"/playbook/active": "/playbookActive",
"/playbook/active/:id": "/playbookActive/:id"
}
Note: the mapping in routes.json is goes like this: [expanded/endpoint]: aliasEndpoint where aliasEndpoint should match the one from db.json.
package.json
{
...
"scripts": {
"api": "json-server [path-to-db.json] --routes [path-to-routes.json] --no-cors=false"
},
...
}
Verify the routing on start (logs from npm run api):
Resources
http://localhost:3000/playbookActive
Other routes
/playbook/active -> /playbookActive
/playbook/active/:id -> /playbookActive/:id
Home
http://localhost:3000
Examples
GET → http://localhost:3000/playbook/active
Response contains a list with all the active playbooks:
[
{
"id": 1,
"name": "Active Playbook 1",
"description": "Description 1"
},
{
"id": 2,
"name": "Active Playbook 2",
"description": "Description 2"
}
]
GET → http://localhost:3000/playbook/active/2
Response contains the active playbook that matches id=2:
{
"id": 2,
"name": "Active Playbook 2",
"description": "Description 2"
}

Check the error message:
Oops, found / character in database property 'dossier/la'.
/ aren't supported, if you want to tweak default routes, see
https://github.com/typicode/json-server/#add-custom-routes
It seems that slashes are not supported.
The solution is to create a routes.json file containing the mapping for your url.
For example the contents of this file could be:
{
my-awesome-endpoint": "playbook/active"
}

For example:
db.json
"list": [
{
"name": "abcde",
"tel": "123454323",
"id": 5
}
]
routes.json
{
"/v1/list?type=hot": "/list"
}
start command:
npx json-server --watch db.json --routes routes.json

Related

Update a field in nested array of objects containing another array of objects in mogoDb (JavaScript)

I have the following structure of Student collection in MongoDb:
{
"_id": "st1",
"student_courses": [
{
"_id": "c1",
"course_name": "Node",
"image": [
{
"_id": "c1img1",
"image": "img1.jpeg"
}
]
},{
"_id": "c2",
"course_name": "React",
"image": [
{
"_id": "c2img2",
"image": "img2.jpeg"
}
]
}
],
}
Now, I want to update the image name of img1.jpeg in all the documents that have same image name. So what I am doing is this:
Student.updateMany(
{ "student_courses._id": "c1"},
{
$set: {
"student_courses.$.course_name": "Node Crash Course",
"student_courses.$.image[0].image": complete_image_name,
},
}
);
Unexpectedly, this is updating course_name field but image. I have tried using $ positional argument instead of [0] but got the error Too many positional arguments ...... I don't know how to do that. My expected output should look like this:
{
"_id": "st1",
"student_courses": [
{
"_id": "c1",
"course_name": "Node Crash Course",
"image": [
{
"_id": "c1img1",
"image": "complete_image_name.jpeg"
}
]
},
:::::::::::::::::::
:::::::::::::::::::
],
}
{
"_id": "st2",
"student_courses": [
{
"_id": "c1",
"course_name": "Node Crash Course",
"image": [
{
"_id": "c1img1",
"image": "complete_image_name.jpeg"
}
]
},
:::::::::::::::::::
:::::::::::::::::::
],
}
Moreover, I have implemented almost every method posted in similar questions. Thanks in advance for any help.
Try to change the way you are referring to the first element in the array:
Student.updateMany(
{ "student_courses._id": "c1"},
{
$set: {
"student_courses.$.course_name": "Node Crash Course",
"student_courses.$.image.0.image": complete_image_name,
},
}
);

How to display REST API response in Vue.js

I am new to Front-end dev, and I want to display below REST API response using Vue.js
Where the domains names will be a DDL, once u select one of them it will show u the list of subdomain in it with their version.
[
{
"subdomains": [
{
"name": "subdomain-1",
"version": "0.4.12"
},
{
"name": "subdomain-2",
"version": "0.4.25"
}
],
"name": "RB"
},
{
"subdomains": [
{
"name": "subdomain-3",
"version": "0.4.2"
},
{
"name": "subdomain-4",
"version": "0.4.5"
}
],
"name": "OB"
}
]

Typeerror pushing javascript object key to new object

I get a TypeError on one particular line (commented) when I run this code:
(I've only included this specific block of code, variables like chunk and count seen here are defined elsewhere).
//if semantic meaning found
if (msg.tmp.nlu.results.semantic_roles.length > 0) {
msg.tmp.nlu.results.semantic_roles.forEach( function (arrayItem){
chunk[count].semantic.push(
{
subject: arrayItem.subject.text,
object: arrayItem.object.text, // TypeError: Cannot read property 'text' of undefined"
action: arrayItem.action.text,
}
);
score++;
});
}
This is the msg.tmp.nlu.results.semantic_roles array I run the forEach on:
[{
"subject": {
"text": "I"
},
"sentence": "I worked with the team to deploy a new marketing software application.",
"object": {
"text": "to deploy a new marketing software application",
"keywords": [{
"text": "new marketing software"
}, {
"text": "application"
}]
},
"action": {
"verb": {
"text": "work",
"tense": "past"
},
"text": "worked",
"normalized": "work"
}
}, {
"subject": {
"text": "I"
},
"sentence": "I worked with the team to deploy a new marketing software application.",
"object": {
"text": "a new marketing software application",
"keywords": [{
"text": "new marketing software"
}, {
"text": "application"
}]
},
"action": {
"verb": {
"text": "deploy",
"tense": "future"
},
"text": "to deploy",
"normalized": "to deploy"
}
}]
It's something to do with the arrayItem.object.text using the name 'object' but have no clue how to resolve it. Can anyone help?
I cannot reproduce your problem, but I've tried to re-create code snippet based on your question. For me it should work correctly, without an TypeError: Cannot read property 'text' of undefined" error. Hopefuly you can use this working code to find and fix problem in yours.
// simulates chunk[count].semantic array
const semantic = [];
// simulates semantic_roles array
const semantic_roles = [{
"subject": {
"text": "I"
},
"sentence": "I worked with the team to deploy a new marketing software application.",
"object": {
"text": "to deploy a new marketing software application",
"keywords": [{
"text": "new marketing software"
}, {
"text": "application"
}]
},
"action": {
"verb": {
"text": "work",
"tense": "past"
},
"text": "worked",
"normalized": "work"
}
}, {
"subject": {
"text": "I"
},
"sentence": "I worked with the team to deploy a new marketing software application.",
"object": {
"text": "a new marketing software application",
"keywords": [{
"text": "new marketing software"
}, {
"text": "application"
}]
},
"action": {
"verb": {
"text": "deploy",
"tense": "future"
},
"text": "to deploy",
"normalized": "to deploy"
}
}];
// Slightly changed forEach loop
semantic_roles.forEach( function (arrayItem){
// Push items to the semantic array
semantic.push(
{
subject: arrayItem.subject.text,
object: arrayItem.object.text,
action: arrayItem.action.text,
}
);
// Log in console arrayItem.object.text for each item
console.log('Text:', arrayItem.object.text);
});
//Log in console semantic_roles array
console.log('semantic_roles array:', semantic_roles); // 2 items
//Log in console semantic array
console.log('semantic array:', semantic); // 2 items

Extract information from json

So I have the following object from my controller, which has a name, a list of beans and a list of operations:
{
"name": "Charge",
"beans": [
],
"operations": [
{
"name": "getSize",
"returnType": "java.lang.Integer",
"description": "empty description",
"parameters": [
]
},
{
"name": "truncate",
"returnType": "java.lang.Void",
"description": "empty description",
"parameters": [
]
},
{
"name": "count",
"returnType": "java.lang.Integer",
"description": "empty description",
"parameters": [
{
"name": "javaCode",
"type": "java.lang.String",
"value": null
}
]
},
{
"name": "update",
"returnType": "java.lang.Integer",
"description": "empty description",
"parameters": [
{
"name": "javaSelectCode",
"type": "java.lang.String",
"value": null
},
{
"name": "javaUpdateCode",
"type": "java.lang.String",
"value": null
}
]
},
{
"name": "delete",
"returnType": "java.lang.Integer",
"description": "empty description",
"parameters": [
{
"name": "javaCode",
"type": "java.lang.String",
"value": null
}
]
},
{
"name": "dump",
"returnType": "java.lang.Void",
"description": "empty description",
"parameters": [
{
"name": "javaSelectCode",
"type": "java.lang.String",
"value": null
},
{
"name": "destinationPath",
"type": "java.lang.String",
"value": null
}
]
},
{
"name": "select",
"returnType": "java.lang.String",
"description": "empty description",
"parameters": [
{
"name": "javaCode",
"type": "java.lang.String",
"value": null
}
]
}
],
"$$hashKey": "object:620"
}
Basically I want to display all the operations from this object in a dropdown menu.
So I was thinking of having something like:
<div ng-repeat="operation in object.operations">
{{operation.name}}
</div>
Except the code above doesn't display anything on the screen, no errors in the console, nothing.
Any help would be much appreciated!
EDIT:
Javascript service:
app.controller('selectAll', ['$http', '$scope' , '$rootScope', function ($http, $scope, $rootScope) {
$scope.response;
$scope.operations;
$rootScope.$on("invokeSelectAll", function(){
$scope.invokeSelectAll();
});
$scope.invokeSelectAll = function(){
$scope.response = $http.post('/invoke/selectAll/', $rootScope.dataObj);
$scope.object = JSON.stringify($rootScope.object);
console.log(" object operation from selectAll " + $scope.object);
$scope.response.then(function(data) {
$scope.responses = data.data ? data.data : "Select Operation not supported on this bean";
});
}
}]);
Screenshot of dev console:
https://imgur.com/a/8WAAL
Use JSON.stringify() to create a JSON string from a JavaScript object.
Use JSON.parse() to parse a JSON string to a JavaScript object.
In your case, you need to use JSON.parse() because you get a JSON string from the server and want to parse it to a JavaScript object.
$scope.object = JSON.parse($rootScope.object);
you are using JSON.stringify which is used to change javascript object to string and store it as a string only.
You should Parse the data with JSON.parse(), and the data becomes a JavaScript object. and you can easily use that in ng-repeat.
Try it ,It will work fine

JSON.parse returning "scanEscape a"

I have QML LocalStorage using javaScript. In it I put object via JSON.stringify(). When I try to read the object from DB using JSON.parse() is returning : scanEscape a and I did't found a reference to this scan error JSON file:
{
"header": {
"version": "2.5",
"createdIn": "PickWorks - Linux",
"modifiedIn": "PickWorks.appName",
"modified": "2013/12/07"
},
"properties": {
"title": "We found a love",
"authors": [
{
"name": "Rihana"
}
],
"transposition": -2,
"tempo": {
"type": "bpm",
"value": "130"
},
"key": "C",
"version": "2.5.4",
"publisher": "GuitarTab",
"keywords": [
"find",
"love",
"deny"
],
"verseOrder": "v1 b c v2 b c",
"themes": [
"love",
"hopeless"
]
},
"lyrics": [
{
"title": "v1",
"text": "[a]Yellow diamonds [F]in the light\n[C]And we're standing [G]side by side\n[a]As your shadow [F]crosses mine\n[C]What it takes to [G]come [a]alive.[F]\n",
"items": {}
},
{
"title": "v2",
"text": "[a]Shine a light through [F]an open door\n[C]Love and life [G]I will divide\n[a]Turn away cause [F]I need you more\n[C]Feel the heart-[G]beat in my [a]mind.[F]\n"
},
{
"title": "c",
"text": "[a]We found love in a [F]hopeless place\n[C]We found love in a [G]hopeless place\n[a]We found love in a [F]hopeless place\n[C]We found love in a [G]hopeless place\n"
},
{
"title": "b",
"text": "[C]It's the way I'm feeling [G]I just can't [a]deny.[F]\n[C]But I've gotta [G]let it go\n"
}
]
}
Q1: What is this error ?
Q2: How to solve it?
P.S.: (Tested on Qt 5.2b & 5.1.1)
Bug was actually some missed debugging log in Qt. Upon creating Bugtrack issue problem was adressed and will be solved in next stable release (Qt 5.2.0).

Categories