I have got the error when I’m trying to remove the Document from Customers Collection.
Then I’ve got the error below:
Exception while invoking method ‘customers.remove’ Error: Match error: Expected string, got object
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';
import { Table, Alert, Button } from 'react-bootstrap';
import { Meteor } from 'meteor/meteor';
import { createContainer } from 'meteor/react-meteor-data';
import { Customers } from '../../api/customers';
const handleRemove = (customerId) => {
if (confirm('Are you sure? This is permanent!')) {
Meteor.call('customers.remove', customerId, (error) => {
if (error) {
console.log('danger');
} else {
console.log('success');
}
});
}
};
const STCustomers = ({ customers, match, history }) => (
<div className="Customers">
<div className="page-header clearfix">
<h4 className="pull-left">Customers</h4>
<Link className="btn btn-success pull-right" to={`${match.url}/new`}>Add Customer</Link>
</div>
{customers.length ? <Table responsive>
<thead>
<tr>
<th>Customer ID</th>
<th>First Name</th>
<th>Last Name</th>
<th />
<th />
</tr>
</thead>
<tbody>
{customers.map(({ _id, custId, firstname, lastname }) => (
<tr key={_id}>
<td>{custId}</td>
<td>{firstname}</td>
<td>{lastname}</td>
<td>
<Button
bsStyle="primary"
onClick={() => history.push(`${match.url}/${_id}`)}
block
>View</Button>
</td>
<td>
<Button
bsStyle="danger"
onClick={() => handleRemove(_id)}
block
>Delete</Button>
</td>
</tr>
))}
</tbody>
</Table> : <Alert bsStyle="warning">No customers yet!</Alert>}
</div>
);
STCustomers.propTypes = {
customers: PropTypes.arrayOf(PropTypes.object).isRequired,
match: PropTypes.object.isRequired,
history: PropTypes.object.isRequired,
};
export default createContainer(() => {
const subscription = Meteor.subscribe('customers');
return {
loading: !subscription.ready(),
customers: Customers.find({}).fetch(),
};
}, STCustomers);
imports/api/customers.js
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
export const Customers = new Mongo.Collection('customers');
Meteor.methods({
'customers.insert': function customersInsert(cust) {
check(cust, {
custId: String,
firstname: String,
lastname: String,
phoneno: String,
email: String,
address: String,
});
try {
return Customers.insert({ owner: this.userId, ...cust });
} catch (exception) {
throw new Meteor.Error('500', exception);
}
},
'customers.update': function customersUpdate(cust) {
check(cust, {
_id: String,
custId: String,
firstname: String,
lastname: String,
phoneno: String,
email: String,
address: String,
});
try {
const customerId = cust._id;
Customers.update(customerId, { $set: cust });
return customerId;
} catch (exception) {
throw new Meteor.Error('500', exception);
}
},
'customers.remove': function customersRemove(customerId) {
check(customerId, String);
try {
return Customers.remove(customerId);
} catch (exception) {
throw new Meteor.Error('500', exception);
}
},
});
Please help to fix this error…