将照片网址存储在数据库中

问题描述 投票:0回答:1

我正在使用Facebook API登录我的反应应用程序。前端是reactJS,后端是nodeJS。首次登录时,我会向nodeJS发送姓名,电子邮件和个人资料照片网址。 Prolem是nodeJS没有以适当的方式解析url,即使它是从reactJS(使用POST)正确发送的。

它看起来像这样:

{'{"name":"testname","email":"[email protected]","profilePhoto":"https://platform-lookaside.fbsbx.com/platform/profilepic/?asid': '1111111111111111', height: '50', width: '50', ext: '1111111111', hash: 'AeRNlZmAF4w5mT5r"}' }

应该是这样的:

{"name":"testname","email":"[email protected]","profilePhoto":"https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=1111111111111111&height=50&width=50&ext=1111111111&hash=ArS0tErHD8Tg9l3s"}"

所以换句话说它不应该将url拆分为部分,因为我想在数据库中拥有原始url。

这是server.js

const express = require("express");
const config = require("../config");
const knex = require("../knex/knex");
const api = require("./api");
const cors = require("cors");
const bodyParser = require("body-parser");

const app = express();

app.use(cors());
app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  next();
});

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use("/api", api.router);

app.listen(config.port, config.host, () => {
  console.log(Server is running on http://${config.host}:${config.port});
});

这是reactJS中的apiRequest.js:

const apiRequest = (apiPath, options) => {
  const mainOptions = {
    headers: {
      Accept: "application/json"
    },
    mode: "cors",
    credentials: "include"
  };

  const finalOptions = merge(mainOptions, options);

  if (finalOptions.body) {
    finalOptions.body = JSON.stringify(finalOptions.body);
    finalOptions.headers["Content-Type"] = "application/x-www-form-urlencoded";
  }

  const requestUrl = `http://${backendConfig.host}:${
    backendConfig.port
  }/api/${apiPath}`;
  console.log(requestUrl, finalOptions);
  return fetch(requestUrl, finalOptions).then(response =>
    solveErrors(response)
  );
};

这种方法是好还是有更好的方法将URL存储在数据库中?

node.js json reactjs facebook-login
1个回答
0
投票

解决了!

我删除了credentials: "include"并将"application/x-www-form-urlencoded"更改为"application/json"

© www.soinside.com 2019 - 2024. All rights reserved.