2

I'm trying to do the Angular2/Meteor tutorial using my own type 'class' instead of 'party' and I'm getting errors on this line of code

classes.forEach((class: Class) => Classes.insert(class));

Here's the whole file:

import { Classes } from '../../../both/collections/classes.collection';
import { Class } from '../../../both/models/class.model';

export function loadClasses() {
 if (Classes.find().cursor.count() === 0) {
const classes: Class[] = [{
  name: 'Class1',
  teacher: 'Dumbledore',
  location: 'room 101'
}, {
  name: 'Class2',
  teacher: 'Hagrid',
  location: 'Online'
}, {
  name: 'Class3',
  teacher: 'Harry Potter',
  location: 'room 124'
}];

classes.forEach((class: Class) => Classes.insert(class));

 }

}

Here is the error code among others that say the same thing for the most part.

server/imports/fixtures/classes.ts (20, 54): Argument of type 'typeof (Anonymous class)' is not assignable to parameter of type 'Class'.

Not sure if it is just being read wrong and I need to reset something. I have combed through my code multiple times and can't find any syntax/spelling errors. I tried 'meteor reset' just in case but no luck with that.

1
  • so I have this class.model.ts file export interface Class { name: string; teacher: string; location: string; } is that what you mean? Commented Jan 23, 2017 at 0:15

2 Answers 2

1

I suspect Typescript does not like you using the ES6 reserved word class (lowercase) as an argument name.

Try replacing it by something else (e.g. currentClass or whatever except another reserved word).

Using Class (uppercase first letter) as a class or interface name is legal, but for sure it will call for this very mistake of using the reserved word class in the wrong place sooner or later.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes the reserved word was the issue, makes it a real pain when making a class planner app haha.
5 years later, your answer helped me. Cheers
0

i think i found the mistake you are doing. this is the code where you are doing mistake

classes.forEach((class: Class) => Classes.insert(class));

in this classes.forEach should be apply on a array that you want to insert into database. and inside this loop you want to insert each class into your database table. and you have taken both array name and database collection name as classes which is wrong. how it differentiate ?

try something like this code

if (Head.find().cursor.count() === 0) {
      const heads = [
      { head: 'javascript'}, 
      { head: 'angularjs'}
      ];
    heads.forEach((defaulthead) => Head.insert(defaulthead));
  }

and i am agree with ghybs answer. you should not use reserved word. Reserved words (like JavaScript keywords) cannot be used as names

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.