尝试访问API调用时发生Javascript 404错误

尝试访问API调用时发生Javascript 404错误,javascript,node.js,knex.js,Javascript,Node.js,Knex.js,我试图更改数据库中的某些数据,但调用api请求后,我会收到错误: Uncaught (in promise) SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) 和我的api文件(删除了所有不相关的内容): 这最终意味着调用我的js文件来更改数据数据库条目: import { defaultTo } from 'lodash'; import guuid from '../../../.

我试图更改数据库中的某些数据,但调用api请求后,我会收到错误:

Uncaught (in promise) SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
和我的api文件(删除了所有不相关的内容):

这最终意味着调用我的js文件来更改数据数据库条目:

import { defaultTo } from 'lodash';
import guuid from '../../../../../../core/guuid';
import authenticateAdmin from '../authenticateAdmin';
import order from '../../../../client/reducers/ui/modals/order';

export default ({ knex }) =>
  authenticateAdmin(knex)(async (req, res) => {
    try {
      console.log('checkinCustomer');
      const { orderId } = req.params;
      const { isCheckedIn } = req.body;

      console.log(orderId);
      console.log(isCheckedIn);

      await knex('orders').where('is_checked_in', '=', orderId).update({ is_checked_in: isCheckedIn }).where({ id: orderId });

      res.status(201).end();
    } catch (err) {
      console.error(err.stack || err);
    }
  });
如果有人能发现我的代码中根本错误的地方,我可以向您保证文件路径都是正确的,所有函数都是彼此可见的,也许这就是我解析数据的方式

编辑

我认为包括我也得到了一个CORS错误可能是有用的:

访问位于的XMLHttpRequest'http://localhost:3000/api/orders/c7216fc0-1197-4cb6-99d4-15760f00b6e7/签入“来源”我的站点名称已被CORS策略阻止:

进一步编辑


我已经设法删除了原始的JSON错误,但是我仍然得到了
404
网络错误以及原始的CORS错误。。。除此之外,如果我将fetch from
checkIn
的最后一部分更改为
cancel
,这是一个完全工作的api调用,那么同样的错误仍然存在。

在向身体传递数据时不应使用
JSON.stringify()


当您使用
application/json
时,您应该在体内传递json格式,我有一个解决方案!结果是我的
从“../../../core/fetch/fetch.server”导入fetch;

初始文件中的
错误,应该从“../../../../core/fetch/fetch”导入fetch

为了使CRO能够看到这一点,我最终删除了我的json正文,因为我真正需要的是在url内部作为参数,也删除了
application/json
的使用,仍然会遇到同样的问题。
import { Router } from 'express';


import checkIn from '../handlers/api/orders/checkInCustomer';


export default (resources) => {
  const router = new Router();

  router.post('/orders/:orderId/checkIn', checkIn(resources));



  return router;
};
import { defaultTo } from 'lodash';
import guuid from '../../../../../../core/guuid';
import authenticateAdmin from '../authenticateAdmin';
import order from '../../../../client/reducers/ui/modals/order';

export default ({ knex }) =>
  authenticateAdmin(knex)(async (req, res) => {
    try {
      console.log('checkinCustomer');
      const { orderId } = req.params;
      const { isCheckedIn } = req.body;

      console.log(orderId);
      console.log(isCheckedIn);

      await knex('orders').where('is_checked_in', '=', orderId).update({ is_checked_in: isCheckedIn }).where({ id: orderId });

      res.status(201).end();
    } catch (err) {
      console.error(err.stack || err);
    }
  });