Javascript 为什么我的POST请求不更新正在提供的.json或.js文件?

Javascript 为什么我的POST请求不更新正在提供的.json或.js文件?,javascript,rest,express,graphql,crud,Javascript,Rest,Express,Graphql,Crud,我知道我错过了一些简单的东西。对我宽容点 我有一个graphQL后端,用于以下功能: const arr = [ { id: 1, foo: 'foo' }, { id: 2, foo: 'bar' }] 然后我通过buildSchema()发出graphql变异请求 在我的rootResolver中,我配置了: var root = { getFooQuery: getFooFunc, getFoosQuery: getFoosFunction, updateFooV

我知道我错过了一些简单的东西。对我宽容点

我有一个graphQL后端,用于以下功能:

const arr = [ { id: 1, foo: 'foo' }, { id: 2, foo: 'bar' }]
然后我通过buildSchema()发出graphql变异请求

在我的rootResolver中,我配置了:

var root = {
    getFooQuery: getFooFunc,
    getFoosQuery: getFoosFunction,
    updateFooValue: updateFooFunc,
};
然后我将更新为:

var updateFooFunc = function ({ id, foo }) {
    arr.map(each => {
        if (each.id === id) {
            each.foo = foo;
            return each;
        }
    });
    return arr.filter(each => each.id === id)[0];
}
这一切实际上在localhost/graphiql用户界面中运行良好,但当我检查数组时,它没有更新

昨天使用fetch/REST post请求时出现了类似的问题。localhost/JSON和immediate-fetch-req很好,但原始的.JSON文件保持不变。显然,这意味着重新启动服务器=您丢失了任何新帐户/新聊天MSG或其他任何东西-显然这不是正确的方法


我错过了什么?

这里有几件事需要记住

当您启动服务器时,像
arr
这样的变量只有在服务器运行时才会保存在内存中。对变量值的更改只会更改内存中的内容,而不会更新实际代码中的内容。当您停止服务器时,变量值将从内存中释放。如果服务器再次启动,这些变量将再次具有您给它们的初始值

通常,如果要持久化数据,需要将其写入数据库或其他数据存储(如Redis)并从中读取。您还可以直接读取/写入文件(有关如何在节点中执行此操作的基本概述,请参阅)

另一方面,同样重要的是要记住,像
filter
map
这样的数组方法不会改变它们所调用的数组的原始值

const array = [1, 2, 3, 4]
array.map(item => item * 2)
console.log(array) // still shows [1, 2, 3, 4]
array.filter(item => item > 3)
console.log(array) // still shows [1, 2, 3, 4]
如果要更改原始值,需要执行以下操作:

let array = [1, 2, 3, 4] // use let since our value will not be *constant*
array = array.map(item => item * 2)
console.log(array) // now shows [2, 4, 6, 8]
array.filter(item => item > 3)
console.log(array) // now shows [4, 6, 8]
const fs = require('fs')

const updateFooFunc = ({ id, foo }) => {
  // assuming foo.json exists
  const valueFromFile = JSON.parse(fs.readFileSync('./foo.json'))
  const newValue = valueFromFile.map(each => {
    if (each.id === id) each.foo = foo
    return each
  })
  fs.writeFileSync(JSON.stringify('./foo.json', newValue))
  // "find" is a little better than "filter" for what you're doing
  return newValue.find(each => each.id === id) 
}
您还可以像这样链接您的方法

array = array.map(item => item * 2).filter(item => item > 3)
综上所述,如果您希望您的冲突解决程序只是从文件中读取和写入,它将如下所示:

let array = [1, 2, 3, 4] // use let since our value will not be *constant*
array = array.map(item => item * 2)
console.log(array) // now shows [2, 4, 6, 8]
array.filter(item => item > 3)
console.log(array) // now shows [4, 6, 8]
const fs = require('fs')

const updateFooFunc = ({ id, foo }) => {
  // assuming foo.json exists
  const valueFromFile = JSON.parse(fs.readFileSync('./foo.json'))
  const newValue = valueFromFile.map(each => {
    if (each.id === id) each.foo = foo
    return each
  })
  fs.writeFileSync(JSON.stringify('./foo.json', newValue))
  // "find" is a little better than "filter" for what you're doing
  return newValue.find(each => each.id === id) 
}

另外,使用根值是传入解析器的一种非常有限的方法。当您继续探索GraphQL时,我强烈建议您最终使用
GraphQL工具
,并使用
makeExecutableSchema
来构建您的模式。谢谢@DanielRearden-我还以为我应该做些别的事情,但我以前忽略了。但你说得很对。如果您想更新静态js/json文件,您需要涉猎fs/writeFile/Node'y的内容。我写了一些类似的东西,现在表现很好。非常感谢。