Cannot find function variable name in angularjs - angularjs

I've created following AngularJS code, but I've found Error: Can't find variable: modifyCustomerAgent error when I render call modifyCustomer() my code. Please let me know what's wrong. Thanks.
function modifyCustomer() {
return modifyCustomerAgent(modifyCustomerData, true).then(function(data) {
console.log(JSON.stringify(data));
});
}
$scope.modifyCustomerAgent = (saveFormData, isUpgrade) => {
return {
property: 'value'
};
}

Try something like this
$scope.modifyCustomerAgent = (saveFormData, isUpgrade) => {
return Promise((resolve,reject)=>{
resolve ({
property: 'value'
});
})
}

Related

ANGULAR Components array key in result get value by id

this.crudService.get('user.php?mode=test')
.subscribe((data:any) => {
{ for (var key in data) { this[key] = data[key]; } };
}
);
This use to work on angular 7 now on angular 13 i get this error (look image)
In template i was using the values for example in json string was and array and i had users, in template was {{users}} , {{posts}} etc.. now the this[key] give error , please help me out its very important can't find solution
i'll show an example code, and then applied to your code:
Example
// creating global variables to receive the values
users: any = null;
posts: any = null;
// simulating the data you will receive
data: any[] = [
{users: ['user1', 'user2', 'user3']},
{posts: ['post1', 'post2', 'post3']}
];
getCrudService() {
// access each object of the array
this.data.forEach(obj => {
// getting keys name and doing something with it
Object.keys(obj).forEach(key => {
// accessing global variable and setting array value by key name
this[String(key)] = obj[String(key)]
})
})
}
Apllied to your code
this.crudService.get('user.php?mode=test').subscribe((data:any) => {
data.forEach(obj => {
Object.keys(obj).forEach(key => {
this[String(key)] = obj[String(key)]
});
});
});
I hope it helped you, if you need help, just reply me.

Cannot read property 'emit' of undefined when trying to emit a document

I am trying to create a design for tags of entities in PouchDB with ReactJS. I managed to save my design using the put function, but when I query my design, the response is just an empty array and I am getting following error in console:
TypeError: Cannot read property 'emit' of undefined
I think the problem is in my function that I later use as a map parameter to my design variable:
function emitTagsMap(doc)
{
if (doc !== undefined)
{
if (Array.isArray(doc.tags))
{
doc.tags.forEach(x =>
{
/* Here is probably the problem - this.db is undefined */
this.db.emit(x, null);
});
}
}
};
this.db is declared in constructor:
constructor(service, name)
{
if (!service || !name) throw new Error("PouchDatabase initialized incorrectly");
this.name = name;
this.db = new PouchDB(name);
this.service = service;
this.tagsView();
}
Please bare in mind that I am completely new to PouchDB.
Any ideas how can I initialize the emit function?
Thank you in advance.
I assume, that your function is a part of a JavaScript class (otherwise you have to explain the idea with this). In ES6, you have to bind this to your regular functions. You have two options:
First - bind it via constructor:
constructor() {
this.emitTagsMap = this.emitTagsMap.bind(this);
}
Second - declare the function as an arrow one. This way, react will bind it for you:
emitTagsMap = (doc) =>
{
if (doc !== undefined)
{
if (Array.isArray(doc.tags))
{
doc.tags.forEach(x =>
{
/* Here is probably the problem - this.db is undefined */
this.db.emit(x, null);
});
}
}
};
You don't need to call emit over the database object.
Try this:
function emitTagsMap(doc)
{
if (doc !== undefined)
{
if (Array.isArray(doc.tags))
{
doc.tags.forEach(x =>
{
emit(x, null);
});
}
}
};
According to the PouchDB docs a design document is formed like this:
// first create a new design doc and pass your map function as string into it
var ddoc = {
_id: "_design/my_index",
views: {
by_name: {
map: "function (doc) { if (doc !== undefined) { if (Array.isArray(doc.tags)) { doc.tags.forEach(x => { emit(x, null); }); } } }"
}
}
};
// save it
db.put(ddoc).then(function () {
// success!
}).catch(function (err) {
// some error (maybe a 409, because it already exists?)
});
//Then you actually query it, by using the name you gave the design document when you saved it:
db.query('my_index/by_name').then(function (res) {
// got the query results
}).catch(function (err) {
// some error
});
https://pouchdb.com/guides/queries.html

Flutter contacts_service package use

I am using https://pub.dev/packages/contacts_service.
I am able to do:
contacts.forEach((contact){
print(contact.displayName);
});
How do I print items of contact.emails ?
They are defined as Iterable
Try this;
contacts.forEach((contact) {
print(contact.displayName);
contact.emails.forEach((item) {
print(item.value);
});
});
Emails are stored in a label: value format in that lib. This should work:
contacts.forEach((contact) {
print('Name: ${contact.displayName}');
for (var email in contact.emails) {
print('Email: ${email.label} => ${email.value}');
}
});

How to create array of objects from http.get

I have http.get and it's impossible for me to create array of objects. Can you help me ? I need to create loop on array of returned objects and i can't do this.
export class Link {
idStat: String;
idAccount: String;
}
links: Link [];
router.get('/linkGetAll', function(req, res, next) {
Link.find(function (err, products) {
if (err) return next(err);
res.json(products);
});
});
getAllLinks(){
return this.http.get('/main/linkGetAll');
}
this.api.getAllLinks().subscribe((data) => {
this.links = data;
})
for(let item in this.links)
{
DOESN'T WORK
}
getAllLinks(){
return this.http.get('/main/linkGetAll')
.pipe(map(res => {
// do something here
// res.forEach() or res.map()
return 'it';
})
);
}
Before going to
for(let item in this.links)
{
DOESN'T WORK
}
Ensure that this.links has data in it.(use async/await along with toPromise())
Please read this section about HTTP of the Angular tutorial and the Angular API.
The get method accepts a type:
get<T>(...): Observable<T>
so you could modify your implementation of getAllLinks:
getAllLinks(): Observable<Link[] {
return this.http.get<Link[]>('/main/linkGetAll');
}
Now you should should be able to iterate the response:
getAllLinks().subscribe(links => {
links.forEach(link => // do what you need);
}

Angular: What's the correct way to return Observable?

I have the following method which isn't working correct:
getProducts(): Observable<Product[]> {
let PRODUCTS: Product[];
this.http.get(this.base_url + "api/products")
.subscribe(
(data) => {
for(var i in data) {
PRODUCTS.push(new Product(data[i].id, data[i].name, data[i].category, data[i].description, data[i].price, data[i].amount));
}
},
(error) => {
console.log(error);
});
return of(PRODUCTS);
}
The error I'm getting is this:
TypeError: Cannot read property 'push' of undefined
Now, I know that the PRODUCT array is not accessable from within the subscribe function, but I cannot get the correct solution for it.
Can anyone help me with that. I want to return an Observable<Product[]>.
Thank you in advance!
Edit: Updated to account for the fact that the API seems to return an array-like object rather than a true array.
You want to use map:
getProducts(): Observable<Product[]> {
return this.http.get(this.base_url + "api/products")
.map(data => {
let products = [];
for (let i in data) {
products.push(new Product(data[i].id, data[i].name, data[i].category, data[i].description, data[i].price, data[i].amount));
}
return products;
})
.do(null, console.log);
}
Since #pixelbit's comment keeps getting upvotes despite being wrong, here's an example showing why it is wrong:
// Fakes a HTTP call which takes half a second to return
const api$ = Rx.Observable.of([1, 2, 3]).delay(500);
function getProducts() {
let products = [];
api$.subscribe(data => {
for (let i in data) {
products.push(data[i]);
}
});
return Rx.Observable.of(products);
}
// Logs '[]' instead of '[1, 2, 3]'
getProducts().subscribe(console.log);

Resources