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

时间:2019-03-20 00:12:31

标签: node.js json reactjs facebook-login

我正在使用Facebook API在我的react应用程序中登录。前端是reactJS,后端是nodeJS。 首次登录时,我将名称,电子邮件和个人资料照片url发送到nodeJS。问题是,即使nodeJS是通过reactJS(使用POST)正确发送的,nodeJS也无法以正确的方式解析URL。

它看起来像这样:

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

应如下所示:

{"name":"testname","email":"test@mail.com","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的更好方法?

1 个答案:

答案 0 :(得分:0)

解决了!

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