Gulp serve > Parsing error: Unexpected identifier - angularjs

I am getting this parsing error whenever I try to gulp serve.
This is the code for which gulp is giving error. I have written a function which takes up a object of FormData type and checks for any of its value to be undefined and if it is then set it to blank string.
function setUndefinedFields(data) {
for (var pair of data.entries()) {
if (pair[1] === 'undefined') {
data.set(pair[0], '');
}
}
return data;
}
But when I perform gulp serve it gives parsing error: unexpected identifier in terminal and in browser console it shows this error
Although if I comment the function perform gulp serve and uncomment the function, then the code works fine.
Edit :
This function is called after appending form data values
var data = new FormData();
data.append('jobTitle', vacancy.jobTitle);
data.append('compulsoryReqs', vacancy.compulsoryReqs);
data.append('preferredReqs', vacancy.preferredReqs);
data.append('company', vacancy.company);
data = setUndefinedFields(data);
Now if any of the field value is undefined the function will replace the value with ''.

Related

console error when running npm start command in new react project

I would like you to help me repair an error that I have, is that every time I create a new project in react with the command npx create-react-app app-name and followed by a cd to the directory and an npm start, I says everything is correct:
Compiled successfully!
You can now view cacas in the browser.
Local: http://localhost:3000
On Your Network: http://172.27.248.214:3000
Note that the development build is not optimized.
To create a production build, use yarn build.
enter image description here
Followed by this I go to the browser, and I give f12 to start the inspector, and I run into the following errors:
First error
Error handling response: SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at chrome-extension://bmjmipppabdlpjccanalncobmbacckjn/js/content/content.js:60:21
According to the error is in this line
data = JSON.parse(data.cs_template);
Full code shown
document.addEventListener('DOMContentLoaded', function () {
for (var i = 0; i < blacklist.length; i++) {
if (location.href.search(blacklist[i]) > 0)
return;
}
cStyle = document.createElement('style');
cStyle.id = 'cs_cursor_css_no_hd';
document.head.appendChild(cStyle);
if (!document.getElementById('elem_ext')) {
elem_ext = document.createElement('div');
elem_ext.id = 'elem_ext';
elem_ext.style.display = 'none';
document.documentElement.appendChild(elem_ext);
}
chrome.storage.local.get('storage_data', function (data) {
if (localStorage.hasOwnProperty('storage_data'))
localStorage.removeItem('storage_data');
localStorage.setItem('storage_data', data.storage_data);
document.documentElement.dataset.csCursorMode = hd_mode;
insert_cs_script();
})
chrome.storage.local.get('cs_template', function (data) {
data = JSON.parse(data.cs_template);
js = document.createElement('script');
js.id = 'cs_cursor_template';
js.innerHTML = data.template;
document.head.appendChild(js);
});
})
Image showing what I already wrote above
enter image description here
Second error
Uncaught SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at main.js:97
The error sends me to this part of code
var storage_data = (localStorage.hasOwnProperty('storage_data')) ? JSON.parse(localStorage.getItem('storage_data')) : {};
enter image description here
Third error
Uncaught ReferenceError: showCsCursorNonHD is not defined
at <anonymous>:1:35
The third error shows me this
cs_hd_mode = false; showCsCursorNonHD(); hideCsCursor();
enter image description here
It is a new project, without any change
enter image description here
Directories
enter image description here
enter image description here
I have been using WSL2 for more than a year and about a month ago these errors began to appear when creating a new project in react
I believe the issue is that you are trying to call JSON.parse(...) on something that is already a JS object. your data in the callback to chrome.storage.local.get('cs_template',...) is already a JS object. I am not sure what the structure of your data is, but data should be the actual cs_template object that can be accessed like data.cs_template in the callback function.

TypeError: undefined is not a constructor in jasmine unit test with angularjs

I am having above error in my Jesmine tests. Code has a Controller which calls AppsService service to receive a promise response of array of apps followed by onLoad method -
AppsService.initAndGetApps(appCredentialType)
.then(onLoad)
.catch(onError);
Code works fine but getting error for unit tests like -
TypeError: undefined is not a constructor (evaluating 'AppsService.initAndGetApps(appCredentialType).then(onLoad)')
I tried mocking initAndGetApps in my spec file with Jasmine & also as custom method like this (both are giving same above error) -
1.
initAndGetApps : jasmine.createSpy("initAndGetApps").and.callFake(function (credentialType) {
return []; // empty is ok
}
2.
AppsService = {
initAndGetApps: function (credentialType) {
return [];
}
}
$provide.value('AppsService', AppsService);
AppsService uses deferred.promise to return promise response after doing some computation based on credentialType parameter (It also does two Promise calls). Test is not able to call initAndGetApps as not getting console of credentialType at first line of initAndGetApps.
Can someone guide me the right way to mock AppsService.
You are not returning a promise in none of those two mocks. I would try if something along these lines:
initAndGetApps : jasmine.createSpy("initAndGetApps").and.returnValue(
new Promise(function(resolve, reject) {
// not taking our time to do the job
resolve(desiredReturnValue);
}
);
You can change the return value to a mock value so your code works.

Angular compile returned data and then trust it not working

Im getting some data and then trying to compile it and then "trust" the content as HTML and then assign the data to scope.model but its for some reason yelling at me saying: Attempted to trust a non-string value in a content requiring a string: Context: html
if (response.data) {
var trustedData = $sce.trustAsHtml(response.data.PostContent);
scope.model.postContent = $compile(trustedData);
}

how to read images names form cordova.file.tempDirectory using Ng-Cordova

We want read images names form cordova.file.tempDirectory using file plugin we tried like this
$cordovaFile.readAsText(cordova.file.tempDirectory)
.then(function (success) {
console.log("success2"+success);
}, function (error) {
console.log("error"+error);
});
But We got error
ERROR: Wrong type for parameter "path" of DirectoryEntry.getFile: Expected String, but got Undefined.
2016-08-25 09:23:28.958 DPOD[3482:38292] Error in Success callbackId: File1548160221 : TypeError: Wrong type for parameter "path" of DirectoryEntry.getFile: Expected String, but got Undefined.
cordova.file.tempDirectory have only 10 images.So we need 10images name.tell me what wrong in our code.Please guide to us.
$cordova.readAsText accepts two arguments path and file.
// READ
$cordovaFile.readAsText(cordova.file.dataDirectory,$scope.inputs.readFile)
.then(function (success) {
// success
}, function (error) {
// error
});
Try reading the file as an array buffer. See other options for reading files here. ngCordova file plugin
Make sure to add ngCordova file and appropriate dependency injections. Installation guide

Angular resource.$query() and error condition returning response as an array however need as plain object

I am using a service to return list of files. This service returns following.
Success case: List of file object in json format as given below with HTTP code as 200
[{"_id:"3453534","name":"File 1"},{"_id:"5756753","name":"File 2"}]
Failure case: This returns error response with error message as HTTP error code 500.
{errorMessage: "No files found for ptoject id 522b9358e4b0bab2f88a1f67"}
I am using following "query" method invoke this service.
//get the new value of files as per changed project id
scope.files = ProjectFile.query({ projectid: scope.project._id }, function (response) {
//Check if service response is success/fail
if (response.servicestatus != undefined) {
//this is error case. Show error message.
alert("Failed to load list of files for this project: " + response.errorMessage);
}
else {
//update scope
scope.files = response;
}
});
Problem I am facing is response object which is converted by $query() method. I am getting response as an array which seems incorrect. Not sure why $query() is also expecting error response as array. In error case; I am getting response as array as below
response[0][0]="N";
response[0][1]="o";
response[0][2]="";
response[0][3]="f";
response[0][4]="i";
response[0][5]="l";
response[0][6]="e";
response[0][7]="s";
....
....
I am not sure why $query() is behaving like this. Not sure how to handle it in correct way. Please help.
The query method of $resource expects the response as an array by default. The query isn't failing, but the response from the server isn't providing what query() is expecting.
If you know that only "no files" responses will not be arrays, then you can check to see if the response type is an array in order to know whether anything has been found or not.
This answer outlines an easy way to check:
scope.files = ProjectFile.query({ projectid: scope.project._id }, function (response) {
//Check if service response is success/fail
if (response.servicestatus != undefined) {
//this is error case. Show error message.
alert("Failed to load list of files for this project: " + response.errorMessage);
}
else if ( Object.prototype.toString.call(response) === '[object Array]' ) {
scope.files = response;
} else {
//handle the lack of files found
}
}

Resources