如何在谷歌功能节点JS上修复“无法处理请求”

时间:2019-03-29 21:38:02

标签: node.js ajax google-cloud-platform request google-cloud-datastore

所以..我在这里有一个问题,我创建了多个函数。我的目标是使用简单的AJAX请求调用它们。 例如:

function createPost(title,description,category,subcategory,tags){
let data = {"kind":"Posts","key":""+title+"","value":{"category":""+category+"","description":""+description+"","subcategory":""+subcategory+"",
"tags":"{'tags' : [{'name':'Lince','color':'blue'},{'name':'BUG','color':'orange'}]}"}};
$.ajax({
    type: "POST",
    url: "https://"+REGION+"-"+PROJECT_ID+".cloudfunctions.net/"+DATASTORE_CREATE_POST+"",
    data: JSON.stringify(data), 
    crossDomain: true,
    dataType: 'json',
    contentType: 'application/json',
    success: function(response) {
          console.log(response);
    },
    error: function(response) {
              console.log(response);
    },
});}

我具有此功能来保存新的Kind密钥:

exports.set = (req, res) => {
//SET CORS
res.set('Access-Control-Allow-Origin', "*")
res.set('Access-Control-Allow-Methods', '*')
res.set('Access-Control-Allow-Headers','*')
// The value contains a JSON document representing the entity we want to save
if (!req.body.value) {
  throw new Error(
    'Value not provided. Make sure you have a "value" property in your request'
  );}
const key = getKeyFromRequestData(req.body);
  const entity = {
    key: key,
    data: req.body.value,
  };

  return datastore
    .save(entity)
    .then(() => res.status(200).send(`Entity ${key.path.join('/')} saved.`))
    .catch(err => {
      console.error(err);
      res.status(500).send(err.message);
      return Promise.reject(err);
    });
};

如果在Google网站上测试此功能,则结果为: Image

但是,如果我想像我之前所说的那样在AJAX请求上调用相同的函数,我会得到:

  

错误:无法处理请求

0 个答案:

没有答案