Thanks robfallows to help me fix remove function issues of my project, then I successfully implement insert function.
Now I have another error on findOne the log is Uncaught TypeError: Customers.findOne is not a function
Please help me to fix view function, I have the following code:
ViewCustomer.js
import React from 'react';
import PropTypes from 'prop-types';
import { ButtonToolbar, ButtonGroup, Button } from 'react-bootstrap';
import { createContainer } from 'meteor/react-meteor-data';
import { Meteor } from 'meteor/meteor';
import Customers from '../../api/customers';
import NotFound from './NotFound';
const handleRemove = (customerId, history) => {
if (confirm('Are you sure? This is permanent!')) {
Meteor.call('customers.remove', customerId, (error) => {
if (error) {
console.log('danger');
} else {
console.log('success');
history.push('/customers');
}
});
}
};
const renderCustomer = (cust, match, history) => (cust ? (
<div className="ViewCustomer">
<div className="page-header clearfix">
<h4 className="pull-left">{ cust && cust.firstname }</h4>
<ButtonToolbar className="pull-right">
<ButtonGroup bsSize="small">
<Button onClick={() => history.push(`${match.url}/edit`)}>Edit</Button>
<Button onClick={() => handleRemove(cust._id, history)} className="text-danger">Delete</Button>
</ButtonGroup>
</ButtonToolbar>
</div>
{ cust && cust.firstname }
</div>
) : <NotFound />);
const ViewCustomer = ({ cust, match, history }) => (
renderCustomer(cust, match, history)
);
ViewCustomer.propTypes = {
cust: PropTypes.object.isRequired,
match: PropTypes.object.isRequired,
history: PropTypes.object.isRequired,
};
export default createContainer(({ match }) => {
const customerId = match.params._id;
const subscription = Meteor.subscribe('customers.view', customerId);
return {
cust: Customers.findOne(customerId) || {},
};
}, ViewCustomer);
publications.js
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { Customers } from '../customers';
Meteor.publish('customers', function customersPublication() {
return Customers.find({});
});
Meteor.publish('customers.view', function customersView(customerId) {
check(customerId, String);
return Customers.find({ _id: customerId });
});