Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将新对象添加到nodeJS中的js文件的数组中_Javascript_Arrays_Express - Fatal编程技术网

Javascript 将新对象添加到nodeJS中的js文件的数组中

Javascript 将新对象添加到nodeJS中的js文件的数组中,javascript,arrays,express,Javascript,Arrays,Express,我只是想知道如何通过express router端点将(推送)数据添加到阵列中。假设我在data/data.js目录中有一个数组,我的路由器代码如下所示: const express=require('express'); const bodyParser=require('body-parser'); const productRouter=express.Router(); //! 带来数据 const{products}=require('../data/data'); 使用(bodyPa

我只是想知道如何通过express router端点将(推送)数据添加到阵列中。假设我在
data/data.js
目录中有一个数组,我的路由器代码如下所示:

const express=require('express');
const bodyParser=require('body-parser');
const productRouter=express.Router();
//! 带来数据
const{products}=require('../data/data');
使用(bodyParser.json());
产品路由器
.路线(“/”)
.post((请求、恢复、下一步)=>{
如果(请求正文!==null){
//一些逻辑
}否则{
产品。推送(要求主体);
res.statusCode=200;
res.setHeader('Content-Type','application/json');
res.json(产品);
}
}否则{
//处理错误
}
});
module.exports=产品路由器;
当我在路由的端点中使用POST方法时,它会推送新数据并使用更新的数组进行响应。但当我检查express文件时,它仍然是旧数组。为什么我丢失了数据
如果有人帮我解决这个问题,我衷心感谢


正如@Aditya toke和@Andre Nuechter建议的那样,我更新代码如下:

.post((请求、恢复、下一步)=>{
如果(请求正文!==null){
if(products.some((product)=>product.id==req.body.id)){
err=新错误(“id为“+req.body.id+”已退出的产品”);
err.status=404;
返回下一个(错误);
}否则{
const data_path=path.join(u dirname,'../data/data.js');
appendFileSync(数据_路径,JSON.stringify(req.body));
res.statusCode=200;
res.setHeader('Content-Type','application/json');
res.json(产品);
}
}否则{
err=新错误('正文未包含产品信息');
err.status=404;
返回下一个(错误);
}
});
但它推送的新数据如下:

exports.products=[
{
标题:"相机",,
imageUrl:'https://source.unsplash.com/gGgoDJRD2WM/300x300',
id:1,
},
...
]
出口订单=[];
{“title”:“mens”,“imageUrl”:”https://source.unsplash.com/1-nx1QR5dTE/300x300,“id”:“6”}

这不是我想要的。有没有办法将其添加到产品阵列中?或者更好的方法?

数据丢失,因为推送不会改变磁盘上的文件。
要做到这一点,您需要使用。

就像您提到的在文件中保存数据一样, 这只能通过使用文件系统在文件中写入数据来实现,
我建议使用JSON扩展文件,因为它易于解析和读取。
您必须使用文件系统在文件中写入数据。
此外,它可以作为整个项目的全局变量使用。
使用node js可以通过多种方式使用文件系统。

更新了答案

@falamiw遵循以下步骤
1.不要使用data.js开始使用data.json
data.json内部的结构如下

{
products : [
  {"title":"mens","imageUrl":"https://source.unsplash.com/1-nx1QR5dTE/300x300","id":"6"},
  {"title":"women","imageUrl":"https://source.unsplash.com/1-nx1QR5dTE/300x300","id":"7"}
          ]
}
在此JSON文件中进行更改的代码

 const fs = require('fs');
    let rawdata = fs.readFileSync('data.json');
   let productArray = JSON.parse(rawdata);
  // push changes to your array
   productArray ['product'].push({ any thing you want to add })
  • 在将对象推入数组之后,现在我们将使用文件系统对data.json文件进行更改

    let data = JSON.stringify(productArray );
    fs.writeFileSync('data.json', data);
    
  • 就这样,现在您可以看到文件中的更改。
    我还没有测试过这段代码,但我相信,只要调试并检查它的工作情况,它就可以工作。

    这是否回答了您的问题?可能会有帮助。我以前没有使用json。如果您提供适合我的条件的类似json的数据结构,这将非常有用。谢谢你,再见toke@falamiw请确认所需数据的结构。产品:[{anything},{anything}]通过这种方式,您将在产品数组中一个接一个地推送数据,对吗?我想添加新的产品对象,如
    {“title”:“…”,“imageUrl”:“…”,“id”:6}
    进入我的产品阵列。@falamiw我已经用您可以检查的每个步骤更新了我的答案,我还建议您,做一些与JSON相关的RND,因为在大多数API中都是编写和处理JSON格式的。让我试试这个,让你知道它是否有效。如果你的回答行得通,我会接受的