- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 MERN 堆栈(Mongo、Express、React、Node)中工作并在我的 API 中遇到错误。
这是我的 plaid.js
文件,其中我的路线之一引发了此错误。当然,我已经删除了所有 secret token 变量,但假设一切正常,直到出现 res.json 错误(确实如此)。
const express = require("express");
const plaid = require("plaid");
const router = express.Router();
const jwt = require("jsonwebtoken");
const keys = require("../../config/keys");
const passport = require("passport");
const moment = require("moment");
const mongoose = require("mongoose");
// Load Account and User models
const Account = require("../../models/Account");
const User = require("../../models/User");
// Replaced my actual keys with empty strings for sake of this post
const PLAID_CLIENT_ID = "";
const PLAID_SECRET = "";
const PLAID_PUBLIC_KEY = "";
const client = new plaid.Client(
PLAID_CLIENT_ID,
PLAID_SECRET,
PLAID_PUBLIC_KEY,
plaid.environments.sandbox,
{ version: "2018-05-22" }
);
var PUBLIC_TOKEN = null;
var ACCESS_TOKEN = null;
var ITEM_ID = null;
// @route POST api/plaid/accounts/add
// @desc Trades public token for access token and stores credentials in database
// @access Private
router.post(
"/accounts/add",
passport.authenticate("jwt", { session: false }),
(req, res) => {
PUBLIC_TOKEN = req.body.public_token;
const userId = req.user.id;
const institution = req.body.metadata.institution;
const { name, institution_id } = institution;
if (PUBLIC_TOKEN) {
client
.exchangePublicToken(PUBLIC_TOKEN)
.then(res => {
ACCESS_TOKEN = res.access_token;
ITEM_ID = res.item_id;
// Check if account already exists for specific user
Account.findOne({
userId: req.user.id,
institutionId: institution_id
})
.then(account => {
if (account) {
console.log("Account already exists");
} else {
const newAccount = new Account({
userId: userId,
publicToken: PUBLIC_TOKEN,
accessToken: ACCESS_TOKEN,
itemId: ITEM_ID,
institutionId: institution_id,
institutionName: name
});
// TO:DO fix error, res.json is not a function
newAccount.save().then(account => res.json(account));
}
})
.catch(err => console.log(err)); // Mongo Error
})
.catch(err => console.log(err)); // Plaid Error
}
}
);
module.exports = router;
newAccount.save()
执行得很好,但后续的 res.json
会抛出错误。这是我返回的错误。也就是说,res.json 不是一个函数
。
[0] (node:23413) UnhandledPromiseRejectionWarning: TypeError: res.json is not a function
[0] at newAccount.save.then.account (/Users/rishi/plaid-auth/routes/api/plaid.js:97:55)
[0] at process._tickCallback (internal/process/next_tick.js:68:7)
这里有很多帖子都提到 res.json is not a function
错误,但所提出的解决方案都不适合我。我很困惑为什么会抛出这个错误,因为我在应用程序的另一部分使用相同的约定,并且 res.json
工作得很好。请参阅下面的示例,了解 res.json
的工作原理。
// @route POST api/posts
// @desc Create a post
// @access Private
router.post(
"/",
passport.authenticate("jwt", { session: false }),
(req, res) => {
const { errors, isValid } = validatePostInput(req.body);
// Check validation
if (!isValid) {
return res.status(400).json(errors);
}
const newPost = new Post({
text: req.body.text,
name: req.body.name,
avatar: req.body.avatar,
user: req.user.id // current logged in user
});
// res.json works just fine
newPost.save().then(post => res.json(post));
}
);
最佳答案
因为这段代码
if (PUBLIC_TOKEN) {
client
.exchangePublicToken(PUBLIC_TOKEN)
.then(res => {
ACCESS_TOKEN = res.access_token;
当你执行这一行newAccount.save().then(account => res.json(account));
时,res不再是函数router上的那个.post('/accounts/add', (req, res) => {})
因此,解决方案很简单,将 promise exchangePublicToken 中的 res 更改为其他内容,如下例所示。
您将 Promise 回调中的参数命名为与上面 router.post 中的回调中的参数相同
if (PUBLIC_TOKEN) {
client
.exchangePublicToken(PUBLIC_TOKEN)
.then(exchangeResponse => {
ACCESS_TOKEN = exchangeResponse.access_token;
关于javascript - Node.js - TypeError : res. json 不是函数(在某些路由中工作,在其他路由中不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53685554/
我正在使用 express并试图通过管道传输 req到远程服务器进行处理,然后通过 res 将结果通过管道传回客户端.它运行良好,但有时我认为 res由 connect-timeout 的操作结束,导
我正在尝试使用 fetch 进行简单的发布请求: const headers = new Headers({ Accept: 'application/json', 'Content-Type
我偶然发现了一个代码示例 here .引起我注意的行(跳过所有其他行): { ... void *res; ... s = pthread_join(tinfo[tnum].thread_id
我有一个 NodeJS 代码,使用请求模块向服务器发出请求。如果我使用“http.request”,代码工作正常,但在 res.on() 上显示错误使用请求来调用电话。以下是显示错误的部分: cons
我想知道这两种不同的方法在 expressjs 中是否相同? res.statusCode = 500; return res.json({ status: "error" }); 或者 r
我正在尝试在 Node.js 中制作简单的 ftp 客户端。一切正常,但我不知道如何在我的 Jade 模板中编写“singleFile.name”。 app.post('/ftp/login', fu
在frameworks/base下有这个xml文件。 /frameworks/base/core/res/res/layout/simple_dropdown_item_2line.xml 如何在我自
我是 odoo v8 的新手,我无法理解 res_partner 和 res_users 表之间的关系以及与 hr_employee 表之间的关系,它们都相关吗? 最佳答案 res.partner之间
我正在开发一个用于用户登录的小部件。基本上,当他们输入有效的帐户详细信息时,我希望它呈现相同的页面(使用 Handlebars )并声明“帐户已成功验证”之类的内容,然后 3 秒后重定向用户。 这是我
我有这种形式的数据 [ { device_id: '12335', timestamp: '2018-05-14T08:31:23.000Z', temperatur: 21,
我是 android 开发的新手,您可能会从我的问题中看出这一点。我发现我有一个 res/menu 文件夹和一个 res/layout 文件夹。它们都包含每个 Activity 的 XML 文件。但是
当我尝试按照指南中的说明在 res/menu 中创建新的菜单资源文件时,该文件夹不存在。 res/xml 也不是。有人可以告诉我如何解决这个问题吗?我尝试创建文件夹,但它不允许我这样做 最佳答案 如果
调用有什么区别: res.flushBuffer(); 对比 res.getOutputStream().flush(); 这些方法是否刷新相同的缓冲区? 如果是这样,你能告诉我这个缓冲区是如何由
为什么以下不起作用? res.send({ successMessage: 'Task saved successfully.' }); res.redirect('/'); 我基本上需要 A
我正在尝试确定是否可以同时调用 res.send(data) 和 res.render('reports')。 为了进一步详细解释,当我路由到“/reports”时,首先在我的服务器端我对返回 jso
app.get('/p/:tagId', function(req, res) { res.send("tagId is set to " + req.param("tagId")); }); 上
我在使用express.js 时遇到一些问题。我想在第一次请求时渲染我的模板,然后从路由中获取一些数据。我的路由如下所示: app.use(function (req, res, next) { r
我想根据屏幕尺寸加载 dimens.xml,但无论屏幕尺寸如何都使用相同的颜色。 我有以下目录结构 res res/values colors.xml res/values-small d
我有一张图片,rectangle.png。它的尺寸是 75x197。它位于 res/drawable 和 res/drawable-xhdpi。它在像这样的网格布局中显示了很多次(截图来自 Nexus
我很好奇返回响应和仅创建响应的区别。 我见过大量使用 return res.status(xxx).json(x) 的代码示例和res.status(xxx).json(x) . 谁能详细解释一下两者
我是一名优秀的程序员,十分优秀!