gpt4 book ai didi

node.js - 插入 JSON 变量

转载 作者:太空宇宙 更新时间:2023-11-04 00:49:45 25 4
gpt4 key购买 nike

我的代码如下:

req.on('data', function(chunk) {
console.log(JSON.stringify(chunk.toString('utf8')));
db.collection('collectionname').insert(chunk.toString('utf8'), function(err, result) {
if (err) throw err;
if (result) console.log("Added!");
});
});

console.log 以 JSON 格式打印正确的消息,但此代码无法将 block 插入 MongoDB。

所以我的问题是如何在 MongoDB 中插入 JSON 变量。提前致谢。

最佳答案

Mongo collection

insert 方法接受文档或文档数组,并向其传递一个字符串。

db.collection('collectionname').insert({
chunk: chunk.toString('utf8')
}, function(err, result){
//...
})

关于node.js - 插入 JSON 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33120449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com